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