Home | History | Annotate | Line # | Download | only in usb
auvitek_dtv.c revision 1.2
      1 /* $NetBSD: auvitek_dtv.c,v 1.2 2011/07/10 00:47:34 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 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 /*
     30  * Auvitek AU0828 USB controller (Digital TV function)
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: auvitek_dtv.c,v 1.2 2011/07/10 00:47:34 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/dtv/dtvif.h>
     50 
     51 #include <dev/usb/auvitekreg.h>
     52 #include <dev/usb/auvitekvar.h>
     53 
     54 static void		auvitek_dtv_get_devinfo(void *,
     55 			    struct dvb_frontend_info *);
     56 static int		auvitek_dtv_open(void *, int);
     57 static void		auvitek_dtv_close(void *);
     58 static int		auvitek_dtv_set_tuner(void *,
     59 			    const struct dvb_frontend_parameters *);
     60 static fe_status_t	auvitek_dtv_get_status(void *);
     61 static uint16_t		auvitek_dtv_get_signal_strength(void *);
     62 static uint16_t		auvitek_dtv_get_snr(void *);
     63 static int		auvitek_dtv_start_transfer(void *);
     64 static int		auvitek_dtv_stop_transfer(void *);
     65 
     66 static int		auvitek_dtv_init_pipes(struct auvitek_softc *);
     67 static int		auvitek_dtv_close_pipes(struct auvitek_softc *);
     68 
     69 static int		auvitek_dtv_bulk_start(struct auvitek_softc *);
     70 static int		auvitek_dtv_bulk_start1(struct auvitek_bulk_xfer *);
     71 static void		auvitek_dtv_bulk_cb(usbd_xfer_handle,
     72 					    usbd_private_handle,
     73 					    usbd_status);
     74 
     75 static const struct dtv_hw_if auvitek_dtv_if = {
     76 	.get_devinfo = auvitek_dtv_get_devinfo,
     77 	.open = auvitek_dtv_open,
     78 	.close = auvitek_dtv_close,
     79 	.set_tuner = auvitek_dtv_set_tuner,
     80 	.get_status = auvitek_dtv_get_status,
     81 	.get_signal_strength = auvitek_dtv_get_signal_strength,
     82 	.get_snr = auvitek_dtv_get_snr,
     83 	.start_transfer = auvitek_dtv_start_transfer,
     84 	.stop_transfer = auvitek_dtv_stop_transfer,
     85 };
     86 
     87 int
     88 auvitek_dtv_attach(struct auvitek_softc *sc)
     89 {
     90 	struct dtv_attach_args daa;
     91 
     92 	daa.hw = &auvitek_dtv_if;
     93 	daa.priv = sc;
     94 	sc->sc_dtvdev = config_found_ia(sc->sc_dev, "dtvbus", &daa, dtv_print);
     95 
     96 	return (sc->sc_dtvdev != NULL);
     97 }
     98 
     99 int
    100 auvitek_dtv_detach(struct auvitek_softc *sc, int flags)
    101 {
    102 	if (sc->sc_dtvdev != NULL) {
    103 		config_detach(sc->sc_dtvdev, flags);
    104 		sc->sc_dtvdev = NULL;
    105 	}
    106 
    107 	return 0;
    108 }
    109 
    110 void
    111 auvitek_dtv_childdet(struct auvitek_softc *sc, device_t child)
    112 {
    113 	if (sc->sc_dtvdev == child)
    114 		sc->sc_dtvdev = NULL;
    115 }
    116 
    117 static void
    118 auvitek_dtv_get_devinfo(void *priv, struct dvb_frontend_info *info)
    119 {
    120 	struct auvitek_softc *sc = priv;
    121 
    122 	memset(info, 0, sizeof(*info));
    123 	strlcpy(info->name, sc->sc_descr, sizeof(info->name));
    124 	info->type = FE_ATSC;
    125 	info->frequency_min = 54000000;
    126 	info->frequency_max = 858000000;
    127 	info->frequency_stepsize = 62500;
    128 	info->caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB;
    129 }
    130 
    131 static int
    132 auvitek_dtv_open(void *priv, int flags)
    133 {
    134 	struct auvitek_softc *sc = priv;
    135 
    136 	if (sc->sc_dying)
    137 		return EIO;
    138 
    139 	mutex_enter(&sc->sc_subdev_lock);
    140 	if (sc->sc_xc5k == NULL) {
    141 		sc->sc_xc5k = xc5k_open(sc->sc_dev, &sc->sc_i2c, 0xc2 >> 1,
    142 		    auvitek_board_tuner_reset, sc,
    143 		    auvitek_board_get_if_frequency(sc),
    144 		    FE_ATSC);
    145 	}
    146 	mutex_exit(&sc->sc_subdev_lock);
    147 
    148 	if (sc->sc_xc5k == NULL)
    149 		return ENXIO;
    150 
    151 	return auvitek_dtv_init_pipes(sc);
    152 }
    153 
    154 static void
    155 auvitek_dtv_close(void *priv)
    156 {
    157 	struct auvitek_softc *sc = priv;
    158 
    159 	auvitek_dtv_stop_transfer(sc);
    160 	auvitek_dtv_close_pipes(sc);
    161 }
    162 
    163 static int
    164 auvitek_dtv_set_tuner(void *priv, const struct dvb_frontend_parameters *params)
    165 {
    166 	struct auvitek_softc *sc = priv;
    167 	int error;
    168 
    169 	error = au8522_set_modulation(sc->sc_au8522, params->u.vsb.modulation);
    170 	if (error)
    171 		return error;
    172 
    173 	delay(100000);
    174 
    175 	au8522_set_gate(sc->sc_au8522, true);
    176 	error = xc5k_tune_dtv(sc->sc_xc5k, params);
    177 	au8522_set_gate(sc->sc_au8522, false);
    178 
    179 	return error;
    180 }
    181 
    182 fe_status_t
    183 auvitek_dtv_get_status(void *priv)
    184 {
    185 	struct auvitek_softc *sc = priv;
    186 
    187 	return au8522_get_dtv_status(sc->sc_au8522);
    188 }
    189 
    190 uint16_t
    191 auvitek_dtv_get_signal_strength(void *priv)
    192 {
    193 	return auvitek_dtv_get_snr(priv);
    194 }
    195 
    196 uint16_t
    197 auvitek_dtv_get_snr(void *priv)
    198 {
    199 	struct auvitek_softc *sc = priv;
    200 
    201 	return au8522_get_snr(sc->sc_au8522);
    202 }
    203 
    204 static int
    205 auvitek_dtv_start_transfer(void *priv)
    206 {
    207 	struct auvitek_softc *sc = priv;
    208 	int s;
    209 
    210 	if (sc->sc_ab.ab_running) {
    211 		return 0;
    212 	}
    213 
    214 	auvitek_write_1(sc, 0x608, 0x90);
    215 	auvitek_write_1(sc, 0x609, 0x72);
    216 	auvitek_write_1(sc, 0x60a, 0x71);
    217 	auvitek_write_1(sc, 0x60b, 0x01);
    218 
    219 	sc->sc_ab.ab_running = true;
    220 
    221 	s = splusb();
    222 	auvitek_dtv_bulk_start(sc);
    223 	splx(s);
    224 
    225 	return 0;
    226 }
    227 
    228 static int
    229 auvitek_dtv_stop_transfer(void *priv)
    230 {
    231 	struct auvitek_softc *sc = priv;
    232 
    233 	sc->sc_ab.ab_running = false;
    234 
    235 	auvitek_write_1(sc, 0x608, 0x00);
    236 	auvitek_write_1(sc, 0x609, 0x00);
    237 	auvitek_write_1(sc, 0x60a, 0x00);
    238 	auvitek_write_1(sc, 0x60b, 0x00);
    239 
    240 	return 0;
    241 }
    242 
    243 static int
    244 auvitek_dtv_init_pipes(struct auvitek_softc *sc)
    245 {
    246 	usbd_status err;
    247 
    248 	err = usbd_open_pipe(sc->sc_bulk_iface, sc->sc_ab.ab_endpt,
    249 	    USBD_EXCLUSIVE_USE, &sc->sc_ab.ab_pipe);
    250 	if (err) {
    251 		aprint_error_dev(sc->sc_dev, "couldn't open bulk-in pipe: %s\n",
    252 		    usbd_errstr(err));
    253 		return ENOMEM;
    254 	}
    255 
    256 	return 0;
    257 }
    258 
    259 static int
    260 auvitek_dtv_close_pipes(struct auvitek_softc *sc)
    261 {
    262 	if (sc->sc_ab.ab_pipe != NULL) {
    263 		usbd_abort_pipe(sc->sc_ab.ab_pipe);
    264 		usbd_close_pipe(sc->sc_ab.ab_pipe);
    265 		sc->sc_ab.ab_pipe = NULL;
    266 	}
    267 
    268 	return 0;
    269 }
    270 
    271 static void
    272 auvitek_dtv_bulk_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
    273     usbd_status status)
    274 {
    275 	struct auvitek_bulk_xfer *bx = priv;
    276 	struct auvitek_softc *sc = bx->bx_sc;
    277 	struct auvitek_bulk *ab = &sc->sc_ab;
    278 	struct dtv_payload payload;
    279 	uint32_t xferlen;
    280 
    281 	if (ab->ab_running == false)
    282 		return;
    283 
    284 	usbd_get_xfer_status(xfer, NULL, NULL, &xferlen, NULL);
    285 
    286 	//printf("%s: status=%d xferlen=%u\n", __func__, status, xferlen);
    287 
    288 	if (status != USBD_NORMAL_COMPLETION) {
    289 		printf("%s: USB error (%s)\n", __func__, usbd_errstr(status));
    290 		if (status == USBD_STALLED) {
    291 			usbd_clear_endpoint_stall_async(ab->ab_pipe);
    292 			goto next;
    293 		}
    294 		if (status == USBD_SHORT_XFER) {
    295 			goto next;
    296 		}
    297 		return;
    298 	}
    299 
    300 	if (xferlen == 0) {
    301 		printf("%s: 0-length xfer\n", __func__);
    302 		goto next;
    303 	}
    304 
    305 	payload.data = bx->bx_buffer;
    306 	payload.size = xferlen;
    307 	dtv_submit_payload(sc->sc_dtvdev, &payload);
    308 
    309 next:
    310 	auvitek_dtv_bulk_start1(bx);
    311 }
    312 
    313 static int
    314 auvitek_dtv_bulk_start(struct auvitek_softc *sc)
    315 {
    316 	int i, error;
    317 
    318 	for (i = 0; i < AUVITEK_NBULK_XFERS; i++) {
    319 		error = auvitek_dtv_bulk_start1(&sc->sc_ab.ab_bx[i]);
    320 		if (error)
    321 			return error;
    322 	}
    323 
    324 	return 0;
    325 }
    326 
    327 static int
    328 auvitek_dtv_bulk_start1(struct auvitek_bulk_xfer *bx)
    329 {
    330 	struct auvitek_softc *sc = bx->bx_sc;
    331 	struct auvitek_bulk *ab = &sc->sc_ab;
    332 	int err;
    333 
    334 	usbd_setup_xfer(bx->bx_xfer, ab->ab_pipe, bx,
    335 	    bx->bx_buffer, AUVITEK_BULK_BUFLEN,
    336 	    //USBD_SHORT_XFER_OK|USBD_NO_COPY, USBD_NO_TIMEOUT,
    337 	    USBD_NO_COPY, 100,
    338 	    auvitek_dtv_bulk_cb);
    339 	err = usbd_transfer(bx->bx_xfer);
    340 	if (err != USBD_IN_PROGRESS) {
    341 		aprint_error_dev(sc->sc_dev, "USB error: %s\n",
    342 		    usbd_errstr(err));
    343 		return ENODEV;
    344 	}
    345 
    346 	return 0;
    347 }
    348