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