Home | History | Annotate | Line # | Download | only in dtv
dtv_device.c revision 1.13
      1 /* $NetBSD: dtv_device.c,v 1.13 2021/07/24 07:48:38 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  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *        This product includes software developed by Jared D. McNeill.
     18  * 4. Neither the name of The NetBSD Foundation nor the names of its
     19  *    contributors may be used to endorse or promote products derived
     20  *    from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  * POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: dtv_device.c,v 1.13 2021/07/24 07:48:38 skrll Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/types.h>
     40 #include <sys/atomic.h>
     41 #include <sys/conf.h>
     42 #include <sys/device.h>
     43 #include <sys/module.h>
     44 #include <sys/poll.h>
     45 #include <sys/select.h>
     46 
     47 #include <dev/dtv/dtvvar.h>
     48 
     49 #include "ioconf.h"
     50 
     51 MODULE(MODULE_CLASS_DRIVER, dtv, NULL);
     52 
     53 static dev_type_open(dtvopen);
     54 static dev_type_close(dtvclose);
     55 static dev_type_read(dtvread);
     56 static dev_type_ioctl(dtvioctl);
     57 static dev_type_poll(dtvpoll);
     58 
     59 const struct cdevsw dtv_cdevsw = {
     60 	.d_open = dtvopen,
     61 	.d_close = dtvclose,
     62 	.d_read = dtvread,
     63 	.d_write = nowrite,
     64 	.d_ioctl = dtvioctl,
     65 	.d_stop = nostop,
     66 	.d_tty = notty,
     67 	.d_poll = dtvpoll,
     68 	.d_mmap = nommap,
     69 	.d_kqfilter = nokqfilter,
     70 	.d_discard = nodiscard,
     71 	.d_flag = D_OTHER | D_MPSAFE,
     72 };
     73 
     74 static int	dtv_match(device_t, cfdata_t, void *);
     75 static void	dtv_attach(device_t, device_t, void *);
     76 static int	dtv_detach(device_t, int);
     77 
     78 CFATTACH_DECL_NEW(dtv,
     79     sizeof(struct dtv_softc),
     80     dtv_match,
     81     dtv_attach,
     82     dtv_detach,
     83     NULL
     84 );
     85 
     86 static int
     87 dtv_match(device_t parent, cfdata_t cfdata, void *aa)
     88 {
     89 	return 1;
     90 }
     91 
     92 static void
     93 dtv_attach(device_t parent, device_t self, void *aa)
     94 {
     95 	struct dtv_attach_args *daa = aa;
     96 	struct dtv_softc *sc = device_private(self);
     97 	struct dtv_stream *ds = &sc->sc_stream;
     98 	struct dvb_frontend_info info;
     99 
    100 	sc->sc_dev = self;
    101 	sc->sc_hw = daa->hw;
    102 	sc->sc_priv = daa->priv;
    103 	sc->sc_open = 0;
    104 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    105 
    106 	ds->ds_nbufs = 0;
    107 	ds->ds_buf = NULL;
    108 	SIMPLEQ_INIT(&ds->ds_ingress);
    109 	SIMPLEQ_INIT(&ds->ds_egress);
    110 	mutex_init(&ds->ds_egress_lock, MUTEX_DEFAULT, IPL_SCHED);
    111 	mutex_init(&ds->ds_ingress_lock, MUTEX_DEFAULT, IPL_SCHED);
    112 	cv_init(&ds->ds_sample_cv, "dtv");
    113 	selinit(&ds->ds_sel);
    114 	dtv_scatter_buf_init(&ds->ds_data);
    115 	if (dtv_buffer_realloc(sc, DTV_DEFAULT_BUFSIZE) != 0) {
    116 		aprint_error(": no memory\n");
    117 		sc->sc_dying = true;
    118 		return;
    119 	}
    120 
    121 	mutex_init(&sc->sc_demux_lock, MUTEX_DEFAULT, IPL_SCHED);
    122 	TAILQ_INIT(&sc->sc_demux_list);
    123 	sc->sc_demux_runcnt = 0;
    124 
    125 	dtv_device_get_devinfo(sc, &info);
    126 
    127 	aprint_naive("\n");
    128 	aprint_normal(": %s", info.name);
    129 	switch (info.type) {
    130 	case FE_QPSK:
    131 		aprint_normal(" [QPSK]");
    132 		break;
    133 	case FE_QAM:
    134 		aprint_normal(" [QAM]");
    135 		break;
    136 	case FE_OFDM:
    137 		aprint_normal(" [OFDM]");
    138 		break;
    139 	case FE_ATSC:
    140 		aprint_normal(" [ATSC]");
    141 		break;
    142 	}
    143 	aprint_normal("\n");
    144 }
    145 
    146 static int
    147 dtv_detach(device_t self, int flags)
    148 {
    149 	struct dtv_softc *sc = device_private(self);
    150 	struct dtv_stream *ds = &sc->sc_stream;
    151 
    152 	cv_destroy(&ds->ds_sample_cv);
    153 	mutex_destroy(&ds->ds_ingress_lock);
    154 	mutex_destroy(&ds->ds_egress_lock);
    155 	seldestroy(&ds->ds_sel);
    156 	dtv_buffer_realloc(sc, 0);
    157 	dtv_scatter_buf_destroy(&ds->ds_data);
    158 
    159 	mutex_destroy(&sc->sc_demux_lock);
    160 	mutex_destroy(&sc->sc_lock);
    161 
    162 	return 0;
    163 }
    164 
    165 #ifdef _MODULE
    166 #include "ioconf.c"
    167 #endif
    168 
    169 static int
    170 dtv_modcmd(modcmd_t cmd, void *arg)
    171 {
    172 #ifdef _MODULE
    173 	int error, bmaj = -1, cmaj = -1;
    174 #endif
    175 
    176 	switch (cmd) {
    177 	case MODULE_CMD_INIT:
    178 #ifdef _MODULE
    179 		error = config_init_component(cfdriver_ioconf_dtv,
    180 		    cfattach_ioconf_dtv, cfdata_ioconf_dtv);
    181 		if (error)
    182 			return error;
    183 		error = devsw_attach("dtv", NULL, &bmaj, &dtv_cdevsw, &cmaj);
    184 		if (error)
    185 			config_fini_component(cfdriver_ioconf_dtv,
    186 			    cfattach_ioconf_dtv, cfdata_ioconf_dtv);
    187 		return error;
    188 #else
    189 		return 0;
    190 #endif
    191 	case MODULE_CMD_FINI:
    192 #ifdef _MODULE
    193 		devsw_detach(NULL, &dtv_cdevsw);
    194 		return config_fini_component(cfdriver_ioconf_dtv,
    195 		    cfattach_ioconf_dtv, cfdata_ioconf_dtv);
    196 #else
    197 		return 0;
    198 #endif
    199 	default:
    200 		return ENOTTY;
    201 	}
    202 }
    203 
    204 static int
    205 dtvopen(dev_t dev, int flags, int mode, lwp_t *l)
    206 {
    207 	struct dtv_softc *sc;
    208 	struct dtv_ts *ts;
    209 	int error;
    210 
    211 	if ((sc = device_lookup_private(&dtv_cd, DTVUNIT(dev))) == NULL)
    212 		return ENXIO;
    213 	if (sc->sc_dying == true)
    214 		return ENODEV;
    215 	ts = &sc->sc_ts;
    216 
    217 	mutex_enter(&sc->sc_lock);
    218 	if (sc->sc_open == 0) {
    219 		error = dtv_device_open(sc, flags);
    220 		if (error)
    221 			return error;
    222 		sc->sc_bufsize = DTV_DEFAULT_BUFSIZE;
    223 		sc->sc_bufsize_chg = true;
    224 		memset(ts->ts_pidfilter, 0, sizeof(ts->ts_pidfilter));
    225 	}
    226 	++sc->sc_open;
    227 	mutex_exit(&sc->sc_lock);
    228 
    229 	if (ISDTVDEMUX(dev))
    230 		return dtv_demux_open(sc, flags, mode, l);
    231 
    232 	return 0;
    233 }
    234 
    235 static int
    236 dtvclose(dev_t dev, int flags, int mode, lwp_t *l)
    237 {
    238 	struct dtv_softc *sc;
    239 
    240 	if ((sc = device_lookup_private(&dtv_cd, DTVUNIT(dev))) == NULL)
    241 		return ENXIO;
    242 
    243 	dtv_common_close(sc);
    244 
    245 	return 0;
    246 }
    247 
    248 static int
    249 dtvread(dev_t dev, struct uio *uio, int flags)
    250 {
    251 	struct dtv_softc *sc;
    252 
    253 	if ((sc = device_lookup_private(&dtv_cd, DTVUNIT(dev))) == NULL)
    254 		return ENXIO;
    255 
    256 	if (ISDTVDVR(dev))
    257 		return dtv_buffer_read(sc, uio, flags);
    258 
    259 	return ENXIO;
    260 }
    261 
    262 static int
    263 dtvioctl(dev_t dev, u_long cmd, void *ptr, int flags, lwp_t *l)
    264 {
    265 	struct dtv_softc *sc;
    266 
    267 	if ((sc = device_lookup_private(&dtv_cd, DTVUNIT(dev))) == NULL)
    268 		return ENXIO;
    269 
    270 	if (ISDTVFRONTEND(dev)) {
    271 		return dtv_frontend_ioctl(sc, cmd, ptr, flags);
    272 	}
    273 
    274 	return EINVAL;
    275 }
    276 
    277 static int
    278 dtvpoll(dev_t dev, int events, lwp_t *l)
    279 {
    280 	struct dtv_softc *sc;
    281 
    282 	if ((sc = device_lookup_private(&dtv_cd, DTVUNIT(dev))) == NULL)
    283 		return POLLERR;
    284 
    285 	if (ISDTVFRONTEND(dev)) {
    286 		return POLLPRI|POLLIN; /* XXX event */
    287 	} else if (ISDTVDVR(dev)) {
    288 		return dtv_buffer_poll(sc, events, l);
    289 	}
    290 
    291 	return POLLERR;
    292 }
    293 
    294 void
    295 dtv_common_close(struct dtv_softc *sc)
    296 {
    297 	mutex_enter(&sc->sc_lock);
    298 	KASSERT(sc->sc_open > 0);
    299 	--sc->sc_open;
    300 	if (sc->sc_open == 0) {
    301 		dtv_device_close(sc);
    302 		dtv_buffer_destroy(sc);
    303 	}
    304 	mutex_exit(&sc->sc_lock);
    305 }
    306