Home | History | Annotate | Line # | Download | only in usb
auvitek_dtv.c revision 1.6.14.4
      1 /* $NetBSD: auvitek_dtv.c,v 1.6.14.4 2015/10/06 21:32:15 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.4 2015/10/06 21:32:15 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 	int err = auvitek_dtv_init_pipes(sc);
    156 	if (err)
    157 		return err;
    158 
    159 	for (size_t i = 0; i < AUVITEK_NBULK_XFERS; i++) {
    160 		sc->sc_ab.ab_bx[i].bx_sc = sc;
    161 		err = usbd_create_xfer(sc->sc_ab.ab_pipe,
    162 		    AUVITEK_BULK_BUFLEN, 0, 0, &sc->sc_ab.ab_bx[i].bx_xfer);
    163 		if (err) {
    164 			aprint_error_dev(sc->sc_dev,
    165 			    "couldn't allocate xfer\n");
    166 			sc->sc_dying = 1;
    167 			return err;
    168 		}
    169 		sc->sc_ab.ab_bx[i].bx_buffer = usbd_get_buffer(
    170 		    sc->sc_ab.ab_bx[i].bx_xfer);
    171 	}
    172 
    173 
    174 	return 0;
    175 }
    176 
    177 static void
    178 auvitek_dtv_close(void *priv)
    179 {
    180 	struct auvitek_softc *sc = priv;
    181 
    182 	auvitek_dtv_stop_transfer(sc);
    183 	auvitek_dtv_close_pipes(sc);
    184 
    185 	for (size_t i = 0; i < AUVITEK_NBULK_XFERS; i++) {
    186 		if (sc->sc_ab.ab_bx[i].bx_xfer)
    187 			usbd_destroy_xfer(sc->sc_ab.ab_bx[i].bx_xfer);
    188 	}
    189 
    190 	sc->sc_dtvsubmitcb = NULL;
    191 	sc->sc_dtvsubmitarg = NULL;
    192 }
    193 
    194 static int
    195 auvitek_dtv_set_tuner(void *priv, const struct dvb_frontend_parameters *params)
    196 {
    197 	struct auvitek_softc *sc = priv;
    198 	int error;
    199 
    200 	error = au8522_set_modulation(sc->sc_au8522, params->u.vsb.modulation);
    201 	if (error)
    202 		return error;
    203 
    204 	delay(100000);
    205 
    206 	au8522_set_gate(sc->sc_au8522, true);
    207 	error = xc5k_tune_dtv(sc->sc_xc5k, params);
    208 	au8522_set_gate(sc->sc_au8522, false);
    209 
    210 	return error;
    211 }
    212 
    213 fe_status_t
    214 auvitek_dtv_get_status(void *priv)
    215 {
    216 	struct auvitek_softc *sc = priv;
    217 
    218 	return au8522_get_dtv_status(sc->sc_au8522);
    219 }
    220 
    221 uint16_t
    222 auvitek_dtv_get_signal_strength(void *priv)
    223 {
    224 	return auvitek_dtv_get_snr(priv);
    225 }
    226 
    227 uint16_t
    228 auvitek_dtv_get_snr(void *priv)
    229 {
    230 	struct auvitek_softc *sc = priv;
    231 
    232 	return au8522_get_snr(sc->sc_au8522);
    233 }
    234 
    235 static int
    236 auvitek_dtv_start_transfer(void *priv,
    237     void (*cb)(void *, const struct dtv_payload *), void *arg)
    238 {
    239 	struct auvitek_softc *sc = priv;
    240 	int s;
    241 
    242 	if (sc->sc_ab.ab_running) {
    243 		return 0;
    244 	}
    245 
    246 	sc->sc_dtvsubmitcb = cb;
    247 	sc->sc_dtvsubmitarg = arg;
    248 
    249 	auvitek_write_1(sc, 0x608, 0x90);
    250 	auvitek_write_1(sc, 0x609, 0x72);
    251 	auvitek_write_1(sc, 0x60a, 0x71);
    252 	auvitek_write_1(sc, 0x60b, 0x01);
    253 
    254 	sc->sc_ab.ab_running = true;
    255 
    256 	s = splusb();
    257 	auvitek_dtv_bulk_start(sc);
    258 	splx(s);
    259 
    260 	return 0;
    261 }
    262 
    263 static int
    264 auvitek_dtv_stop_transfer(void *priv)
    265 {
    266 	struct auvitek_softc *sc = priv;
    267 
    268 	sc->sc_ab.ab_running = false;
    269 
    270 	auvitek_write_1(sc, 0x608, 0x00);
    271 	auvitek_write_1(sc, 0x609, 0x00);
    272 	auvitek_write_1(sc, 0x60a, 0x00);
    273 	auvitek_write_1(sc, 0x60b, 0x00);
    274 
    275 	return 0;
    276 }
    277 
    278 static int
    279 auvitek_dtv_init_pipes(struct auvitek_softc *sc)
    280 {
    281 	usbd_status err;
    282 
    283 	KERNEL_LOCK(1, curlwp);
    284 	err = usbd_open_pipe(sc->sc_bulk_iface, sc->sc_ab.ab_endpt,
    285 	    USBD_EXCLUSIVE_USE|USBD_MPSAFE, &sc->sc_ab.ab_pipe);
    286 	KERNEL_UNLOCK_ONE(curlwp);
    287 
    288 	if (err) {
    289 		aprint_error_dev(sc->sc_dev, "couldn't open bulk-in pipe: %s\n",
    290 		    usbd_errstr(err));
    291 		return ENOMEM;
    292 	}
    293 
    294 	return 0;
    295 }
    296 
    297 static int
    298 auvitek_dtv_close_pipes(struct auvitek_softc *sc)
    299 {
    300 	if (sc->sc_ab.ab_pipe != NULL) {
    301 		KERNEL_LOCK(1, curlwp);
    302 		usbd_abort_pipe(sc->sc_ab.ab_pipe);
    303 		usbd_close_pipe(sc->sc_ab.ab_pipe);
    304 		KERNEL_UNLOCK_ONE(curlwp);
    305 		sc->sc_ab.ab_pipe = NULL;
    306 	}
    307 
    308 	return 0;
    309 }
    310 
    311 static void
    312 auvitek_dtv_bulk_cb(struct usbd_xfer *xfer, void *priv,
    313     usbd_status status)
    314 {
    315 	struct auvitek_bulk_xfer *bx = priv;
    316 	struct auvitek_softc *sc = bx->bx_sc;
    317 	struct auvitek_bulk *ab = &sc->sc_ab;
    318 	struct dtv_payload payload;
    319 	uint32_t xferlen;
    320 
    321 	if (ab->ab_running == false || sc->sc_dtvsubmitcb == NULL)
    322 		return;
    323 
    324 	usbd_get_xfer_status(xfer, NULL, NULL, &xferlen, NULL);
    325 
    326 	//printf("%s: status=%d xferlen=%u\n", __func__, status, xferlen);
    327 
    328 	if (status != USBD_NORMAL_COMPLETION) {
    329 		printf("%s: USB error (%s)\n", __func__, usbd_errstr(status));
    330 		if (status == USBD_STALLED) {
    331 			usbd_clear_endpoint_stall_async(ab->ab_pipe);
    332 			goto next;
    333 		}
    334 		if (status == USBD_SHORT_XFER) {
    335 			goto next;
    336 		}
    337 		return;
    338 	}
    339 
    340 	if (xferlen == 0) {
    341 		printf("%s: 0-length xfer\n", __func__);
    342 		goto next;
    343 	}
    344 
    345 	payload.data = bx->bx_buffer;
    346 	payload.size = xferlen;
    347 	sc->sc_dtvsubmitcb(sc->sc_dtvsubmitarg, &payload);
    348 
    349 next:
    350 	auvitek_dtv_bulk_start1(bx);
    351 }
    352 
    353 static int
    354 auvitek_dtv_bulk_start(struct auvitek_softc *sc)
    355 {
    356 	int i, error;
    357 
    358 	for (i = 0; i < AUVITEK_NBULK_XFERS; i++) {
    359 		error = auvitek_dtv_bulk_start1(&sc->sc_ab.ab_bx[i]);
    360 		if (error)
    361 			return error;
    362 	}
    363 
    364 	return 0;
    365 }
    366 
    367 static int
    368 auvitek_dtv_bulk_start1(struct auvitek_bulk_xfer *bx)
    369 {
    370 	struct auvitek_softc *sc = bx->bx_sc;
    371 	usbd_status err;
    372 
    373 	usbd_setup_xfer(bx->bx_xfer, bx, bx->bx_buffer, AUVITEK_BULK_BUFLEN,
    374 	    0 /* USBD_SHORT_XFER_OK */, 100 /* USBD_NO_TIMEOUT */,
    375 	    auvitek_dtv_bulk_cb);
    376 
    377 	KERNEL_LOCK(1, curlwp);
    378 	err = usbd_transfer(bx->bx_xfer);
    379 	KERNEL_UNLOCK_ONE(curlwp);
    380 
    381 	if (err != USBD_IN_PROGRESS) {
    382 		aprint_error_dev(sc->sc_dev, "USB error: %s\n",
    383 		    usbd_errstr(err));
    384 		return ENODEV;
    385 	}
    386 
    387 	return 0;
    388 }
    389