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