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