2011-07-12 04:57:19 +04:00
|
|
|
/* $NetBSD: dtv_device.c,v 1.4 2011/07/12 00:57:19 jmcneill Exp $ */
|
2011-07-09 18:46:56 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* 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>
|
2011-07-12 04:57:19 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: dtv_device.c,v 1.4 2011/07/12 00:57:19 jmcneill Exp $");
|
2011-07-09 18:46:56 +04:00
|
|
|
|
|
|
|
#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>
|
|
|
|
|
|
|
|
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,
|
2011-07-12 04:57:19 +04:00
|
|
|
.d_write = nowrite,
|
2011-07-09 18:46:56 +04:00
|
|
|
.d_ioctl = dtvioctl,
|
2011-07-12 04:57:19 +04:00
|
|
|
.d_stop = nostop,
|
|
|
|
.d_tty = notty,
|
2011-07-09 18:46:56 +04:00
|
|
|
.d_poll = dtvpoll,
|
2011-07-12 04:57:19 +04:00
|
|
|
.d_mmap = nommap,
|
|
|
|
.d_kqfilter = nokqfilter,
|
2011-07-09 18:46:56 +04:00
|
|
|
.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
|
|
|
|
);
|
|
|
|
|
2011-07-09 23:24:10 +04:00
|
|
|
extern struct cfdriver dtv_cd;
|
|
|
|
|
2011-07-09 18:46:56 +04:00
|
|
|
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);
|
2011-07-12 04:57:19 +04:00
|
|
|
mutex_init(&ds->ds_egress_lock, MUTEX_DEFAULT, IPL_VM);
|
|
|
|
mutex_init(&ds->ds_ingress_lock, MUTEX_DEFAULT, IPL_VM);
|
2011-07-09 18:46:56 +04:00
|
|
|
cv_init(&ds->ds_sample_cv, "dtv");
|
|
|
|
selinit(&ds->ds_sel);
|
|
|
|
dtv_scatter_buf_init(&ds->ds_data);
|
2011-07-09 21:55:20 +04:00
|
|
|
if (dtv_buffer_realloc(sc, DTV_DEFAULT_BUFSIZE) != 0) {
|
|
|
|
aprint_error(": no memory\n");
|
|
|
|
sc->sc_dying = true;
|
|
|
|
return;
|
|
|
|
}
|
2011-07-09 18:46:56 +04:00
|
|
|
|
|
|
|
dtv_device_get_devinfo(sc, &info);
|
|
|
|
|
|
|
|
aprint_naive("\n");
|
2011-07-12 04:57:19 +04:00
|
|
|
aprint_normal(": %s", info.name);
|
2011-07-09 18:46:56 +04:00
|
|
|
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);
|
2011-07-12 04:57:19 +04:00
|
|
|
mutex_destroy(&ds->ds_ingress_lock);
|
|
|
|
mutex_destroy(&ds->ds_egress_lock);
|
2011-07-09 18:46:56 +04:00
|
|
|
seldestroy(&ds->ds_sel);
|
2011-07-09 21:55:20 +04:00
|
|
|
dtv_buffer_realloc(sc, 0);
|
2011-07-09 18:46:56 +04:00
|
|
|
dtv_scatter_buf_destroy(&ds->ds_data);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-09 23:24:10 +04:00
|
|
|
#ifdef _MODULE
|
|
|
|
#include "ioconf.c"
|
|
|
|
#endif
|
|
|
|
|
2011-07-09 18:46:56 +04:00
|
|
|
static int
|
|
|
|
dtv_modcmd(modcmd_t cmd, void *arg)
|
|
|
|
{
|
2011-07-09 23:24:10 +04:00
|
|
|
#ifdef _MODULE
|
2011-07-09 18:46:56 +04:00
|
|
|
int error, bmaj = -1, cmaj = -1;
|
2011-07-09 23:24:10 +04:00
|
|
|
#endif
|
2011-07-09 18:46:56 +04:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case MODULE_CMD_INIT:
|
2011-07-09 23:24:10 +04:00
|
|
|
#ifdef _MODULE
|
2011-07-09 18:46:56 +04:00
|
|
|
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;
|
2011-07-09 23:24:10 +04:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2011-07-09 18:46:56 +04:00
|
|
|
case MODULE_CMD_FINI:
|
2011-07-09 23:24:10 +04:00
|
|
|
#ifdef _MODULE
|
2011-07-09 18:46:56 +04:00
|
|
|
devsw_detach(NULL, &dtv_cdevsw);
|
|
|
|
return config_fini_component(cfdriver_ioconf_dtv,
|
|
|
|
cfattach_ioconf_dtv, cfdata_ioconf_dtv);
|
2011-07-09 23:24:10 +04:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2011-07-09 18:46:56 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return POLLERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
dtv_print(void *arg, const char *pnp)
|
|
|
|
{
|
|
|
|
if (pnp)
|
|
|
|
aprint_normal("dtv at %s", pnp);
|
|
|
|
|
|
|
|
return UNCONF;
|
|
|
|
}
|