NetBSD/sys/dev/dtv/dtv_device.c

277 lines
6.8 KiB
C
Raw Normal View History

/* $NetBSD: dtv_device.c,v 1.2 2011/07/09 17:55:20 jmcneill Exp $ */
/*-
* Copyright (c) 2011 Jared D. McNeill <jmcneill@invisible.ca>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Jared D. McNeill.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: dtv_device.c,v 1.2 2011/07/09 17:55:20 jmcneill Exp $");
#include <sys/types.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/atomic.h>
#include <sys/module.h>
#include <sys/poll.h>
#include <sys/select.h>
#include <dev/dtv/dtvvar.h>
#include <dev/dtv/dtvmodule.h>
MODULE(MODULE_CLASS_DRIVER, dtv, NULL);
static dev_type_open(dtvopen);
static dev_type_close(dtvclose);
static dev_type_read(dtvread);
static dev_type_ioctl(dtvioctl);
static dev_type_poll(dtvpoll);
const struct cdevsw dtv_cdevsw = {
.d_open = dtvopen,
.d_close = dtvclose,
.d_read = dtvread,
.d_ioctl = dtvioctl,
.d_poll = dtvpoll,
.d_flag = D_OTHER|D_MPSAFE,
};
static int dtv_match(device_t, cfdata_t, void *);
static void dtv_attach(device_t, device_t, void *);
static int dtv_detach(device_t, int);
CFATTACH_DECL_NEW(dtv,
sizeof(struct dtv_softc),
dtv_match,
dtv_attach,
dtv_detach,
NULL
);
static int
dtv_match(device_t parent, cfdata_t cfdata, void *aa)
{
return 1;
}
static void
dtv_attach(device_t parent, device_t self, void *aa)
{
struct dtv_attach_args *daa = aa;
struct dtv_softc *sc = device_private(self);
struct dtv_stream *ds = &sc->sc_stream;
struct dvb_frontend_info info;
sc->sc_dev = self;
sc->sc_hw = daa->hw;
sc->sc_priv = daa->priv;
atomic_swap_uint(&sc->sc_open, 0);
ds->ds_nbufs = 0;
ds->ds_buf = NULL;
SIMPLEQ_INIT(&ds->ds_ingress);
SIMPLEQ_INIT(&ds->ds_egress);
mutex_init(&ds->ds_lock, MUTEX_DEFAULT, IPL_VM);
cv_init(&ds->ds_sample_cv, "dtv");
selinit(&ds->ds_sel);
dtv_scatter_buf_init(&ds->ds_data);
if (dtv_buffer_realloc(sc, DTV_DEFAULT_BUFSIZE) != 0) {
aprint_error(": no memory\n");
sc->sc_dying = true;
return;
}
dtv_device_get_devinfo(sc, &info);
aprint_naive("\n");
aprint_normal(": '%s'", info.name);
switch (info.type) {
case FE_QPSK:
aprint_normal(" [QPSK]");
break;
case FE_QAM:
aprint_normal(" [QAM]");
break;
case FE_OFDM:
aprint_normal(" [OFDM]");
break;
case FE_ATSC:
aprint_normal(" [ATSC]");
break;
}
aprint_normal("\n");
}
static int
dtv_detach(device_t self, int flags)
{
struct dtv_softc *sc = device_private(self);
struct dtv_stream *ds = &sc->sc_stream;
cv_destroy(&ds->ds_sample_cv);
mutex_destroy(&ds->ds_lock);
seldestroy(&ds->ds_sel);
dtv_buffer_realloc(sc, 0);
dtv_scatter_buf_destroy(&ds->ds_data);
return 0;
}
static int
dtv_modcmd(modcmd_t cmd, void *arg)
{
int error, bmaj = -1, cmaj = -1;
switch (cmd) {
case MODULE_CMD_INIT:
error = config_init_component(cfdriver_ioconf_dtv,
cfattach_ioconf_dtv, cfdata_ioconf_dtv);
if (error)
return error;
error = devsw_attach("dtv", NULL, &bmaj, &dtv_cdevsw, &cmaj);
if (error)
config_fini_component(cfdriver_ioconf_dtv,
cfattach_ioconf_dtv, cfdata_ioconf_dtv);
return error;
case MODULE_CMD_FINI:
devsw_detach(NULL, &dtv_cdevsw);
return config_fini_component(cfdriver_ioconf_dtv,
cfattach_ioconf_dtv, cfdata_ioconf_dtv);
default:
return ENOTTY;
}
}
static int
dtvopen(dev_t dev, int flags, int mode, lwp_t *l)
{
struct dtv_softc *sc;
struct dtv_ts *ts;
unsigned int n;
int error;
if ((sc = device_lookup_private(&dtv_cd, DTVUNIT(dev))) == NULL)
return ENXIO;
if (sc->sc_dying == true)
return ENODEV;
ts = &sc->sc_ts;
n = atomic_cas_uint(&sc->sc_open, 0, 1);
if (n == 0) {
error = dtv_device_open(sc, flags);
if (error) {
atomic_swap_uint(&sc->sc_open, 0);
return error;
}
sc->sc_bufsize = DTV_DEFAULT_BUFSIZE;
sc->sc_bufsize_chg = true;
memset(ts->ts_pidfilter, 0, sizeof(ts->ts_pidfilter));
}
return 0;
}
static int
dtvclose(dev_t dev, int flags, int mode, lwp_t *l)
{
struct dtv_softc *sc;
if ((sc = device_lookup_private(&dtv_cd, DTVUNIT(dev))) == NULL)
return ENXIO;
dtv_device_close(sc);
dtv_buffer_destroy(sc);
atomic_swap_uint(&sc->sc_open, 0);
return 0;
}
static int
dtvread(dev_t dev, struct uio *uio, int flags)
{
struct dtv_softc *sc;
if ((sc = device_lookup_private(&dtv_cd, DTVUNIT(dev))) == NULL)
return ENXIO;
if (ISDTVDVR(dev))
return dtv_buffer_read(sc, uio, flags);
return ENXIO;
}
static int
dtvioctl(dev_t dev, u_long cmd, void *ptr, int flags, lwp_t *l)
{
struct dtv_softc *sc;
if ((sc = device_lookup_private(&dtv_cd, DTVUNIT(dev))) == NULL)
return ENXIO;
if (ISDTVFRONTEND(dev)) {
return dtv_frontend_ioctl(sc, cmd, ptr, flags);
} else if (ISDTVDEMUX(dev)) {
return dtv_demux_ioctl(sc, cmd, ptr, flags);
}
return EINVAL;
}
static int
dtvpoll(dev_t dev, int events, lwp_t *l)
{
struct dtv_softc *sc;
if ((sc = device_lookup_private(&dtv_cd, DTVUNIT(dev))) == NULL)
return POLLERR;
if (ISDTVFRONTEND(dev)) {
return POLLPRI|POLLIN; /* XXX event */
} else if (ISDTVDEMUX(dev)) {
return POLLIN|POLLOUT; /* XXX */
} else if (ISDTVDVR(dev)) {
return dtv_buffer_poll(sc, events, l);
}
printf("%s: DTVDEV = 0x%x, events = 0x%x\n",
__func__, DTVDEV(dev), events);
return POLLERR;
}
int
dtv_print(void *arg, const char *pnp)
{
if (pnp)
aprint_normal("dtv at %s", pnp);
return UNCONF;
}