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