emdtv_dtv.c revision 1.5.6.1       1  1.5.6.1       mrg /* $NetBSD: emdtv_dtv.c,v 1.5.6.1 2012/02/18 07:35:04 mrg Exp $ */
      2      1.1  jmcneill 
      3      1.1  jmcneill /*-
      4      1.1  jmcneill  * Copyright (c) 2008, 2011 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 #include <sys/cdefs.h>
     30  1.5.6.1       mrg __KERNEL_RCSID(0, "$NetBSD: emdtv_dtv.c,v 1.5.6.1 2012/02/18 07:35:04 mrg Exp $");
     31      1.1  jmcneill 
     32      1.1  jmcneill #include <sys/param.h>
     33      1.1  jmcneill #include <sys/systm.h>
     34      1.1  jmcneill #include <sys/device.h>
     35      1.1  jmcneill #include <sys/conf.h>
     36      1.1  jmcneill 
     37      1.1  jmcneill #include <dev/usb/usb.h>
     38      1.1  jmcneill #include <dev/usb/usbdi.h>
     39      1.1  jmcneill #include <dev/usb/usbdi_util.h>
     40      1.1  jmcneill #include <dev/usb/usbdevs.h>
     41      1.1  jmcneill 
     42      1.1  jmcneill #include <dev/i2c/i2cvar.h>
     43      1.1  jmcneill 
     44      1.1  jmcneill #include <dev/usb/emdtvvar.h>
     45      1.1  jmcneill #include <dev/usb/emdtvreg.h>
     46      1.1  jmcneill 
     47      1.1  jmcneill static void		emdtv_dtv_get_devinfo(void *,
     48      1.1  jmcneill 			    struct dvb_frontend_info *);
     49      1.1  jmcneill static int		emdtv_dtv_open(void *, int);
     50      1.1  jmcneill static void		emdtv_dtv_close(void *);
     51      1.1  jmcneill static int		emdtv_dtv_set_tuner(void *,
     52      1.1  jmcneill 			    const struct dvb_frontend_parameters *);
     53      1.1  jmcneill static fe_status_t	emdtv_dtv_get_status(void *);
     54      1.1  jmcneill static uint16_t		emdtv_dtv_get_signal_strength(void *);
     55      1.1  jmcneill static uint16_t		emdtv_dtv_get_snr(void *);
     56      1.5  jmcneill static int		emdtv_dtv_start_transfer(void *,
     57      1.5  jmcneill 			    void (*)(void *, const struct dtv_payload *),
     58      1.5  jmcneill 			    void *);
     59      1.1  jmcneill static int		emdtv_dtv_stop_transfer(void *);
     60      1.1  jmcneill 
     61      1.1  jmcneill static int		emdtv_dtv_tuner_reset(void *);
     62      1.1  jmcneill 
     63      1.1  jmcneill static void		emdtv_dtv_isoc_startall(struct emdtv_softc *);
     64      1.1  jmcneill static int		emdtv_dtv_isoc_start(struct emdtv_softc *,
     65      1.1  jmcneill 			    struct emdtv_isoc_xfer *);
     66      1.1  jmcneill static void		emdtv_dtv_isoc(usbd_xfer_handle, usbd_private_handle,
     67      1.1  jmcneill 			    usbd_status);
     68      1.1  jmcneill 
     69      1.1  jmcneill static const struct dtv_hw_if emdtv_dtv_if = {
     70      1.1  jmcneill 	.get_devinfo = emdtv_dtv_get_devinfo,
     71      1.1  jmcneill 	.open = emdtv_dtv_open,
     72      1.1  jmcneill 	.close = emdtv_dtv_close,
     73      1.1  jmcneill 	.set_tuner = emdtv_dtv_set_tuner,
     74      1.1  jmcneill 	.get_status = emdtv_dtv_get_status,
     75      1.1  jmcneill 	.get_signal_strength = emdtv_dtv_get_signal_strength,
     76      1.1  jmcneill 	.get_snr = emdtv_dtv_get_snr,
     77      1.1  jmcneill 	.start_transfer = emdtv_dtv_start_transfer,
     78      1.1  jmcneill 	.stop_transfer = emdtv_dtv_stop_transfer,
     79      1.1  jmcneill };
     80      1.1  jmcneill 
     81      1.1  jmcneill void
     82      1.1  jmcneill emdtv_dtv_attach(struct emdtv_softc *sc)
     83      1.1  jmcneill {
     84      1.1  jmcneill 	usb_endpoint_descriptor_t *ed;
     85      1.1  jmcneill 	usbd_status status;
     86      1.1  jmcneill 	int i;
     87      1.1  jmcneill 
     88      1.1  jmcneill 	for (i = 0; i < EMDTV_NXFERS; i++) {
     89      1.1  jmcneill 		sc->sc_ix[i].ix_altix = (i & 1) ?
     90      1.1  jmcneill 		    &sc->sc_ix[i - 1] : &sc->sc_ix[i + 1];
     91      1.1  jmcneill 		sc->sc_ix[i].ix_sc = sc;
     92      1.1  jmcneill 	}
     93      1.1  jmcneill 
     94      1.1  jmcneill 	ed = usbd_interface2endpoint_descriptor(sc->sc_iface, 3);
     95      1.1  jmcneill 	if (ed == NULL) {
     96      1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't find endpoint 3\n");
     97      1.1  jmcneill 		return;
     98      1.1  jmcneill 	}
     99      1.1  jmcneill 	sc->sc_isoc_maxpacketsize = UGETW(ed->wMaxPacketSize);
    100      1.1  jmcneill 	sc->sc_isoc_buflen = sc->sc_isoc_maxpacketsize * EMDTV_NFRAMES;
    101      1.1  jmcneill 
    102      1.1  jmcneill 	aprint_debug_dev(sc->sc_dev, "calling usbd_open_pipe, ep 0x%02x\n",
    103      1.1  jmcneill 	    ed->bEndpointAddress);
    104      1.1  jmcneill 	status = usbd_open_pipe(sc->sc_iface,
    105      1.1  jmcneill 	    ed->bEndpointAddress, USBD_EXCLUSIVE_USE,
    106      1.1  jmcneill 	    &sc->sc_isoc_pipe);
    107      1.1  jmcneill 	if (status != USBD_NORMAL_COMPLETION) {
    108      1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't open isoc pipe\n");
    109      1.1  jmcneill 		usbd_set_interface(sc->sc_iface, 0);
    110      1.1  jmcneill 		return;
    111      1.1  jmcneill 	}
    112      1.1  jmcneill 
    113      1.1  jmcneill 	emdtv_write_1(sc, UR_GET_STATUS, 0x48, 0x00);
    114      1.1  jmcneill 	emdtv_write_1(sc, UR_GET_STATUS, 0x12, 0x77);
    115      1.1  jmcneill 	usbd_delay_ms(sc->sc_udev, 6);
    116      1.1  jmcneill 
    117      1.1  jmcneill 	emdtv_gpio_ctl(sc, EMDTV_GPIO_ANALOG_ON, false);
    118      1.1  jmcneill 	emdtv_gpio_ctl(sc, EMDTV_GPIO_TS1_ON, true);
    119      1.1  jmcneill 	emdtv_gpio_ctl(sc, EMDTV_GPIO_TUNER1_ON, true);
    120      1.1  jmcneill 	emdtv_gpio_ctl(sc, EMDTV_GPIO_DEMOD1_RESET, true);
    121      1.1  jmcneill 	usbd_delay_ms(sc->sc_udev, 100);
    122      1.1  jmcneill 
    123      1.5  jmcneill 	emdtv_dtv_rescan(sc, NULL, NULL);
    124      1.1  jmcneill }
    125      1.1  jmcneill 
    126      1.1  jmcneill void
    127      1.1  jmcneill emdtv_dtv_detach(struct emdtv_softc *sc, int flags)
    128      1.1  jmcneill {
    129      1.1  jmcneill 	sc->sc_streaming = false;
    130      1.1  jmcneill 
    131      1.1  jmcneill 	if (sc->sc_dtvdev != NULL) {
    132      1.1  jmcneill 		config_detach(sc->sc_dtvdev, flags);
    133      1.1  jmcneill 		sc->sc_dtvdev = NULL;
    134      1.1  jmcneill 	}
    135      1.1  jmcneill 
    136      1.1  jmcneill 	if (sc->sc_xc3028)
    137      1.1  jmcneill 		xc3028_close(sc->sc_xc3028);
    138      1.1  jmcneill 	if (sc->sc_lg3303)
    139      1.1  jmcneill 		lg3303_close(sc->sc_lg3303);
    140      1.1  jmcneill 
    141      1.1  jmcneill 	if (sc->sc_isoc_pipe) {
    142      1.1  jmcneill 		usbd_abort_pipe(sc->sc_isoc_pipe);
    143      1.1  jmcneill 		usbd_close_pipe(sc->sc_isoc_pipe);
    144      1.1  jmcneill 		sc->sc_isoc_pipe = NULL;
    145      1.1  jmcneill 	}
    146      1.1  jmcneill }
    147      1.1  jmcneill 
    148      1.5  jmcneill void
    149      1.5  jmcneill emdtv_dtv_rescan(struct emdtv_softc *sc, const char *ifattr, const int *locs)
    150      1.5  jmcneill {
    151      1.5  jmcneill 	struct dtv_attach_args daa;
    152      1.5  jmcneill 
    153      1.5  jmcneill 	daa.hw = &emdtv_dtv_if;
    154      1.5  jmcneill 	daa.priv = sc;
    155      1.5  jmcneill 
    156      1.5  jmcneill 	if (ifattr_match(ifattr, "dtvbus") && sc->sc_dtvdev == NULL)
    157      1.5  jmcneill 		sc->sc_dtvdev = config_found_ia(sc->sc_dev, "dtvbus",
    158      1.5  jmcneill 		    &daa, dtv_print);
    159      1.5  jmcneill }
    160      1.5  jmcneill 
    161      1.1  jmcneill static void
    162      1.1  jmcneill emdtv_dtv_get_devinfo(void *priv, struct dvb_frontend_info *info)
    163      1.1  jmcneill {
    164      1.1  jmcneill 	struct emdtv_softc *sc = priv;
    165      1.1  jmcneill 
    166      1.1  jmcneill 	memset(info, 0, sizeof(*info));
    167      1.1  jmcneill 	strlcpy(info->name, sc->sc_board->eb_name, sizeof(info->name));
    168      1.1  jmcneill 	info->type = FE_ATSC;
    169      1.1  jmcneill 	info->frequency_min = 54000000;
    170      1.1  jmcneill 	info->frequency_max = 858000000;
    171      1.1  jmcneill 	info->frequency_stepsize = 62500;
    172      1.1  jmcneill 	info->caps = FE_CAN_8VSB;
    173      1.1  jmcneill }
    174      1.1  jmcneill 
    175      1.1  jmcneill static int
    176      1.1  jmcneill emdtv_dtv_open(void *priv, int flags)
    177      1.1  jmcneill {
    178      1.1  jmcneill 	struct emdtv_softc *sc = priv;
    179      1.1  jmcneill 
    180      1.1  jmcneill 	if (sc->sc_dying)
    181      1.1  jmcneill 		return ENXIO;
    182      1.1  jmcneill 
    183      1.1  jmcneill 	switch (sc->sc_board->eb_tuner) {
    184      1.2  jakllsch 	case EMDTV_TUNER_XC3028:
    185      1.2  jakllsch 		if (sc->sc_xc3028 == NULL) {
    186      1.2  jakllsch 			sc->sc_xc3028 = xc3028_open(sc->sc_dev,
    187      1.2  jakllsch 			    &sc->sc_i2c, 0x61 << 1, emdtv_dtv_tuner_reset, sc,
    188      1.2  jakllsch 			    XC3028);
    189      1.2  jakllsch 		}
    190      1.2  jakllsch 		if (sc->sc_xc3028 == NULL) {
    191      1.2  jakllsch 			aprint_error_dev(sc->sc_dev, "couldn't open xc3028\n");
    192      1.2  jakllsch 			return ENXIO;
    193      1.2  jakllsch 		}
    194      1.2  jakllsch 		break;
    195      1.1  jmcneill 	case EMDTV_TUNER_XC3028L:
    196      1.1  jmcneill 		if (sc->sc_xc3028 == NULL) {
    197      1.1  jmcneill 			sc->sc_xc3028 = xc3028_open(sc->sc_dev,
    198      1.1  jmcneill 			    &sc->sc_i2c, 0x61 << 1, emdtv_dtv_tuner_reset, sc,
    199      1.1  jmcneill 			    XC3028L);
    200      1.1  jmcneill 		}
    201      1.1  jmcneill 		if (sc->sc_xc3028 == NULL) {
    202      1.1  jmcneill 			aprint_error_dev(sc->sc_dev, "couldn't open xc3028l\n");
    203      1.1  jmcneill 			return ENXIO;
    204      1.1  jmcneill 		}
    205      1.1  jmcneill 		break;
    206      1.1  jmcneill 	default:
    207      1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "unsupported tuner (%d)\n",
    208      1.1  jmcneill 		    sc->sc_board->eb_tuner);
    209      1.1  jmcneill 		return EIO;
    210      1.1  jmcneill 	}
    211      1.1  jmcneill 
    212      1.1  jmcneill 	switch (sc->sc_board->eb_demod) {
    213      1.1  jmcneill 	case EMDTV_DEMOD_LG3303:
    214      1.1  jmcneill 		if (sc->sc_lg3303 == NULL) {
    215      1.1  jmcneill 			sc->sc_lg3303 = lg3303_open(sc->sc_dev,
    216      1.3  jmcneill 			    &sc->sc_i2c, 0x1c, 0);
    217      1.1  jmcneill 		}
    218      1.1  jmcneill 		if (sc->sc_lg3303 == NULL) {
    219      1.1  jmcneill 			aprint_error_dev(sc->sc_dev, "couldn't open lg3303\n");
    220      1.1  jmcneill 			return ENXIO;
    221      1.1  jmcneill 		}
    222      1.1  jmcneill 		break;
    223      1.1  jmcneill 	default:
    224      1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "unsupported demod (%d)\n",
    225      1.1  jmcneill 		    sc->sc_board->eb_demod);
    226      1.1  jmcneill 		return EIO;
    227      1.1  jmcneill 	}
    228      1.1  jmcneill 
    229      1.1  jmcneill 	return 0;
    230      1.1  jmcneill }
    231      1.1  jmcneill 
    232      1.1  jmcneill static void
    233      1.1  jmcneill emdtv_dtv_close(void *priv)
    234      1.1  jmcneill {
    235      1.1  jmcneill 	return;
    236      1.1  jmcneill }
    237      1.1  jmcneill 
    238      1.1  jmcneill static int
    239      1.1  jmcneill emdtv_dtv_set_tuner(void *priv, const struct dvb_frontend_parameters *params)
    240      1.1  jmcneill {
    241      1.1  jmcneill 	struct emdtv_softc *sc = priv;
    242      1.1  jmcneill 	int error;
    243      1.1  jmcneill 
    244      1.1  jmcneill 	/* Setup demod */
    245      1.1  jmcneill 	error = ENXIO;
    246      1.1  jmcneill 	if (sc->sc_lg3303)
    247      1.1  jmcneill 		error = lg3303_set_modulation(sc->sc_lg3303,
    248      1.1  jmcneill 		    params->u.vsb.modulation);
    249      1.1  jmcneill 	if (error)
    250      1.1  jmcneill 		return error;
    251      1.1  jmcneill 
    252      1.1  jmcneill 	/* Setup tuner */
    253      1.1  jmcneill 	error = ENXIO;
    254      1.1  jmcneill 	if (sc->sc_xc3028)
    255      1.1  jmcneill 		error = xc3028_tune_dtv(sc->sc_xc3028, params);
    256      1.1  jmcneill 
    257      1.1  jmcneill 	return error;
    258      1.1  jmcneill }
    259      1.1  jmcneill 
    260      1.1  jmcneill static fe_status_t
    261      1.1  jmcneill emdtv_dtv_get_status(void *priv)
    262      1.1  jmcneill {
    263      1.1  jmcneill 	struct emdtv_softc *sc = priv;
    264      1.1  jmcneill 
    265      1.1  jmcneill 	if (sc->sc_lg3303)
    266      1.1  jmcneill 		return lg3303_get_dtv_status(sc->sc_lg3303);
    267      1.1  jmcneill 
    268      1.1  jmcneill 	return 0;
    269      1.1  jmcneill }
    270      1.1  jmcneill 
    271      1.1  jmcneill uint16_t
    272      1.1  jmcneill emdtv_dtv_get_signal_strength(void *priv)
    273      1.1  jmcneill {
    274      1.4  jmcneill 	struct emdtv_softc *sc = priv;
    275      1.4  jmcneill 
    276      1.4  jmcneill 	if (sc->sc_lg3303)
    277      1.4  jmcneill 		return lg3303_get_signal_strength(sc->sc_lg3303);
    278      1.4  jmcneill 
    279      1.1  jmcneill 	return 0;
    280      1.1  jmcneill }
    281      1.1  jmcneill 
    282      1.1  jmcneill uint16_t
    283      1.1  jmcneill emdtv_dtv_get_snr(void *priv)
    284      1.1  jmcneill {
    285      1.4  jmcneill 	struct emdtv_softc *sc = priv;
    286      1.4  jmcneill 
    287      1.4  jmcneill 	if (sc->sc_lg3303)
    288      1.4  jmcneill 		return lg3303_get_snr(sc->sc_lg3303);
    289      1.4  jmcneill 
    290      1.1  jmcneill 	return 0;
    291      1.1  jmcneill }
    292      1.1  jmcneill 
    293      1.1  jmcneill static int
    294      1.5  jmcneill emdtv_dtv_start_transfer(void *priv,
    295      1.5  jmcneill     void (*cb)(void *, const struct dtv_payload *), void *arg)
    296      1.1  jmcneill {
    297      1.1  jmcneill 	struct emdtv_softc *sc = priv;
    298      1.1  jmcneill 	int i, s;
    299      1.1  jmcneill 
    300      1.1  jmcneill 	s = splusb();
    301      1.1  jmcneill 
    302      1.1  jmcneill 	sc->sc_streaming = true;
    303      1.5  jmcneill 	sc->sc_dtvsubmitcb = cb;
    304      1.5  jmcneill 	sc->sc_dtvsubmitarg = arg;
    305      1.1  jmcneill 
    306      1.1  jmcneill 	aprint_debug_dev(sc->sc_dev, "allocating isoc xfers (pktsz %d)\n",
    307      1.1  jmcneill 	    sc->sc_isoc_maxpacketsize);
    308      1.1  jmcneill 
    309  1.5.6.1       mrg 	KERNEL_LOCK(1, curlwp);
    310      1.1  jmcneill 	for (i = 0; i < EMDTV_NXFERS; i++) {
    311      1.1  jmcneill 		sc->sc_ix[i].ix_xfer = usbd_alloc_xfer(sc->sc_udev);
    312      1.1  jmcneill 		sc->sc_ix[i].ix_buf = usbd_alloc_buffer(sc->sc_ix[i].ix_xfer,
    313      1.1  jmcneill 		    sc->sc_isoc_buflen);
    314      1.1  jmcneill 		aprint_debug_dev(sc->sc_dev, "  ix[%d] xfer %p buf %p\n",
    315      1.1  jmcneill 		    i, sc->sc_ix[i].ix_xfer, sc->sc_ix[i].ix_buf);
    316      1.1  jmcneill 	}
    317  1.5.6.1       mrg 	KERNEL_UNLOCK_ONE(curlwp);
    318      1.1  jmcneill 
    319      1.1  jmcneill 	aprint_debug_dev(sc->sc_dev, "starting isoc transactions\n");
    320      1.1  jmcneill 
    321      1.1  jmcneill 	emdtv_dtv_isoc_startall(sc);
    322      1.1  jmcneill 	splx(s);
    323      1.1  jmcneill 
    324      1.1  jmcneill 	return 0;
    325      1.1  jmcneill }
    326      1.1  jmcneill 
    327      1.1  jmcneill static int
    328      1.1  jmcneill emdtv_dtv_stop_transfer(void *priv)
    329      1.1  jmcneill {
    330      1.1  jmcneill 	struct emdtv_softc *sc = priv;
    331      1.1  jmcneill 	int i;
    332      1.1  jmcneill 
    333      1.1  jmcneill 	aprint_debug_dev(sc->sc_dev, "stopping stream\n");
    334      1.1  jmcneill 
    335      1.1  jmcneill 	sc->sc_streaming = false;
    336      1.1  jmcneill 
    337  1.5.6.1       mrg 	KERNEL_LOCK(1, curlwp);
    338      1.1  jmcneill 	if (sc->sc_isoc_pipe != NULL)
    339      1.1  jmcneill 		usbd_abort_pipe(sc->sc_isoc_pipe);
    340      1.1  jmcneill 
    341      1.1  jmcneill 	for (i = 0; i < EMDTV_NXFERS; i++)
    342      1.1  jmcneill 		if (sc->sc_ix[i].ix_xfer) {
    343      1.1  jmcneill 			usbd_free_xfer(sc->sc_ix[i].ix_xfer);
    344      1.1  jmcneill 			sc->sc_ix[i].ix_xfer = NULL;
    345      1.1  jmcneill 			sc->sc_ix[i].ix_buf = NULL;
    346      1.1  jmcneill 		}
    347  1.5.6.1       mrg 	KERNEL_UNLOCK_ONE(curlwp);
    348      1.1  jmcneill 
    349      1.5  jmcneill 	sc->sc_dtvsubmitcb = NULL;
    350      1.5  jmcneill 	sc->sc_dtvsubmitarg = NULL;
    351      1.5  jmcneill 
    352      1.1  jmcneill 	return 0;
    353      1.1  jmcneill }
    354      1.1  jmcneill 
    355      1.1  jmcneill static void
    356      1.1  jmcneill emdtv_dtv_isoc_startall(struct emdtv_softc *sc)
    357      1.1  jmcneill {
    358      1.1  jmcneill 	int i;
    359      1.1  jmcneill 
    360      1.1  jmcneill 	if (sc->sc_streaming == false || sc->sc_dying == true)
    361      1.1  jmcneill 		return;
    362      1.1  jmcneill 
    363      1.1  jmcneill 	for (i = 0; i < EMDTV_NXFERS; i += 2)
    364      1.1  jmcneill 		emdtv_dtv_isoc_start(sc, &sc->sc_ix[i]);
    365      1.1  jmcneill }
    366      1.1  jmcneill 
    367      1.1  jmcneill static int
    368      1.1  jmcneill emdtv_dtv_isoc_start(struct emdtv_softc *sc, struct emdtv_isoc_xfer *ix)
    369      1.1  jmcneill {
    370      1.1  jmcneill 	int i;
    371      1.1  jmcneill 
    372      1.1  jmcneill 	if (sc->sc_isoc_pipe == NULL)
    373      1.1  jmcneill 		return EIO;
    374      1.1  jmcneill 
    375      1.1  jmcneill 	for (i = 0; i < EMDTV_NFRAMES; i++)
    376      1.1  jmcneill 		ix->ix_frlengths[i] = sc->sc_isoc_maxpacketsize;
    377      1.1  jmcneill 
    378      1.1  jmcneill 	usbd_setup_isoc_xfer(ix->ix_xfer,
    379      1.1  jmcneill 			     sc->sc_isoc_pipe,
    380      1.1  jmcneill 			     ix,
    381      1.1  jmcneill 			     ix->ix_frlengths,
    382      1.1  jmcneill 			     EMDTV_NFRAMES,
    383      1.1  jmcneill 			     USBD_NO_COPY | USBD_SHORT_XFER_OK,
    384      1.1  jmcneill 			     emdtv_dtv_isoc);
    385  1.5.6.1       mrg 
    386  1.5.6.1       mrg 	KERNEL_LOCK(1, curlwp);
    387      1.1  jmcneill 	usbd_transfer(ix->ix_xfer);
    388  1.5.6.1       mrg 	KERNEL_UNLOCK_ONE(curlwp);
    389      1.1  jmcneill 
    390      1.1  jmcneill 	return 0;
    391      1.1  jmcneill }
    392      1.1  jmcneill 
    393      1.1  jmcneill static void
    394      1.1  jmcneill emdtv_dtv_isoc(usbd_xfer_handle xfer, usbd_private_handle priv,
    395      1.1  jmcneill     usbd_status err)
    396      1.1  jmcneill {
    397      1.1  jmcneill 	struct emdtv_isoc_xfer *ix = priv;
    398      1.1  jmcneill 	struct emdtv_softc *sc = ix->ix_sc;
    399      1.1  jmcneill 	struct dtv_payload payload;
    400      1.1  jmcneill 	usbd_pipe_handle isoc = sc->sc_isoc_pipe;
    401      1.1  jmcneill 	uint32_t len;
    402      1.1  jmcneill 	uint8_t *buf;
    403      1.1  jmcneill 	int i;
    404      1.1  jmcneill 
    405      1.1  jmcneill 	KASSERT(xfer == ix->ix_xfer);
    406      1.1  jmcneill 
    407      1.5  jmcneill 	if (sc->sc_dying || sc->sc_dtvsubmitcb == NULL)
    408      1.1  jmcneill 		return;
    409      1.1  jmcneill 
    410      1.1  jmcneill 	if (err) {
    411      1.1  jmcneill 		if (err == USBD_STALLED) {
    412      1.1  jmcneill 			usbd_clear_endpoint_stall_async(isoc);
    413      1.1  jmcneill 			goto resched;
    414      1.1  jmcneill 		}
    415      1.1  jmcneill 		return;
    416      1.1  jmcneill 	}
    417      1.1  jmcneill 
    418      1.1  jmcneill 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
    419      1.1  jmcneill 
    420      1.1  jmcneill 	if (len == 0)
    421      1.1  jmcneill 		goto resched;
    422      1.1  jmcneill 
    423      1.1  jmcneill 	buf = usbd_get_buffer(xfer);
    424      1.1  jmcneill 	if (buf == NULL)
    425      1.1  jmcneill 		goto resched;
    426      1.1  jmcneill 
    427      1.1  jmcneill 	for (i = 0; i < EMDTV_NFRAMES; i++, buf += sc->sc_isoc_maxpacketsize) {
    428      1.1  jmcneill 		if (ix->ix_frlengths[i] == 0)
    429      1.1  jmcneill 			continue;
    430      1.1  jmcneill 		payload.data = buf;
    431      1.1  jmcneill 		payload.size = ix->ix_frlengths[i];
    432      1.5  jmcneill 		sc->sc_dtvsubmitcb(sc->sc_dtvsubmitarg, &payload);
    433      1.1  jmcneill 	}
    434      1.1  jmcneill 
    435      1.1  jmcneill resched:
    436      1.1  jmcneill 	emdtv_dtv_isoc_start(sc, ix->ix_altix);
    437      1.1  jmcneill }
    438      1.1  jmcneill 
    439      1.1  jmcneill static int
    440      1.1  jmcneill emdtv_dtv_tuner_reset(void *opaque)
    441      1.1  jmcneill {
    442      1.1  jmcneill 	struct emdtv_softc *sc = opaque;
    443      1.1  jmcneill 	emdtv_gpio_ctl(sc, EMDTV_GPIO_TUNER1_RESET, true);
    444      1.1  jmcneill 	return 0;
    445      1.1  jmcneill }
    446