Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/dtv add digital TV framework which implements a subs...



details:   https://anonhg.NetBSD.org/src/rev/8f23303dfa9c
branches:  trunk
changeset: 767102:8f23303dfa9c
user:      jmcneill <jmcneill%NetBSD.org@localhost>
date:      Sat Jul 09 14:46:56 2011 +0000

description:
add digital TV framework which implements a subset of Linux DVB APIs

diffstat:

 sys/dev/dtv/Makefile         |    6 +
 sys/dev/dtv/dtv_buffer.c     |  315 +++++++++++++++++++++++++++++++++++++++++++
 sys/dev/dtv/dtv_device.c     |  270 ++++++++++++++++++++++++++++++++++++
 sys/dev/dtv/dtv_ioctl.c      |  146 +++++++++++++++++++
 sys/dev/dtv/dtv_scatter.c    |  256 ++++++++++++++++++++++++++++++++++
 sys/dev/dtv/dtv_scatter.h    |   66 +++++++++
 sys/dev/dtv/dtvif.h          |   80 ++++++++++
 sys/dev/dtv/dtvio.h          |   45 ++++++
 sys/dev/dtv/dtvio_demux.h    |  144 +++++++++++++++++++
 sys/dev/dtv/dtvio_frontend.h |  292 +++++++++++++++++++++++++++++++++++++++
 sys/dev/dtv/dtvmodule.h      |   45 ++++++
 sys/dev/dtv/dtvvar.h         |  115 +++++++++++++++
 sys/dev/dtv/files.dtv        |   10 +
 13 files changed, 1790 insertions(+), 0 deletions(-)

diffs (truncated from 1842 to 300 lines):

diff -r 358d0317c782 -r 8f23303dfa9c sys/dev/dtv/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/dtv/Makefile      Sat Jul 09 14:46:56 2011 +0000
@@ -0,0 +1,6 @@
+# $NetBSD: Makefile,v 1.1 2011/07/09 14:46:56 jmcneill Exp $
+
+INCSDIR= /usr/include/dev/dtv
+INCS=  dtvio.h dtvio_frontend.h dtvio_demux.h
+
+.include <bsd.kinc.mk>
diff -r 358d0317c782 -r 8f23303dfa9c sys/dev/dtv/dtv_buffer.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/dtv/dtv_buffer.c  Sat Jul 09 14:46:56 2011 +0000
@@ -0,0 +1,315 @@
+/* $NetBSD: dtv_buffer.c,v 1.1 2011/07/09 14:46:56 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2011 Jared D. McNeill <jmcneill%invisible.ca@localhost>
+ * 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_buffer.c,v 1.1 2011/07/09 14:46:56 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/kernel.h>
+#include <sys/types.h>
+#include <sys/conf.h>
+#include <sys/device.h>
+#include <sys/vnode.h>
+#include <sys/poll.h>
+#include <sys/select.h>
+
+#include <dev/dtv/dtvvar.h>
+
+#define        PAGE_ALIGN(a)   (((a) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))
+
+static void
+dtv_buffer_write(struct dtv_softc *sc, const uint8_t *buf, size_t buflen)
+{
+       struct dtv_stream *ds = &sc->sc_stream;
+       struct dtv_buffer *db;
+       struct dtv_scatter_io sio;
+       size_t resid = buflen, avail;
+               off_t offset = 0;
+
+       KASSERT(buflen == TS_PKTLEN);
+
+       while (resid > 0) {
+               mutex_enter(&ds->ds_lock);
+
+               if (SIMPLEQ_EMPTY(&ds->ds_ingress)) {
+                       aprint_debug_dev(sc->sc_dev,
+                           "dropping sample (%u)\n", resid);
+                       mutex_exit(&ds->ds_lock);
+                       return;
+               }
+
+               db = SIMPLEQ_FIRST(&ds->ds_ingress);
+               avail = min(db->db_length - db->db_bytesused, resid);
+               if (dtv_scatter_io_init(&ds->ds_data,
+                   db->db_offset + db->db_bytesused, avail, &sio)) {
+                       dtv_scatter_io_copyin(&sio, buf + offset);
+                       db->db_bytesused += (avail - sio.sio_resid);
+                       offset += (avail - sio.sio_resid);
+                       resid -= (avail - sio.sio_resid);
+               }
+
+               if (db->db_bytesused == db->db_length) {
+                       SIMPLEQ_REMOVE_HEAD(&ds->ds_ingress, db_entries);
+                       SIMPLEQ_INSERT_TAIL(&ds->ds_egress, db, db_entries);
+                       cv_broadcast(&ds->ds_sample_cv);
+                       selnotify(&ds->ds_sel, 0, 0);
+               }
+
+               mutex_exit(&ds->ds_lock);
+       }
+}
+
+void
+dtv_submit_payload(device_t self, const struct dtv_payload *payload)
+{
+       struct dtv_softc *sc = device_private(self);
+       struct dtv_ts *ts = &sc->sc_ts;
+       const uint8_t *tspkt;
+       unsigned int npkts, i;
+
+       tspkt = payload->data;
+       npkts = payload->size / TS_PKTLEN;
+       for (i = 0; i < npkts; i++) {
+               if (TS_HAS_SYNC(tspkt) && ts->ts_pidfilter[TS_PID(tspkt)]) {
+                       dtv_buffer_write(sc, tspkt, TS_PKTLEN);
+               }
+               tspkt += TS_PKTLEN;
+       }
+}
+
+static struct dtv_buffer *
+dtv_buffer_alloc(void)
+{
+       return kmem_alloc(sizeof(struct dtv_buffer), KM_SLEEP);
+}
+
+static void
+dtv_buffer_free(struct dtv_buffer *db)
+{
+       kmem_free(db, sizeof(*db));
+}
+
+static int
+dtv_buffer_realloc(struct dtv_softc *sc, size_t bufsize)
+{
+       struct dtv_stream *ds = &sc->sc_stream;
+       unsigned int i, nbufs, oldnbufs, minnbufs;
+       struct dtv_buffer **oldbuf;
+       off_t offset;
+       int error;
+
+       nbufs = PAGE_ALIGN(bufsize) / PAGE_SIZE;
+
+       error = dtv_scatter_buf_set_size(&ds->ds_data, bufsize);
+       if (error)
+               return error;
+
+       oldnbufs = ds->ds_nbufs;
+       oldbuf = ds->ds_buf;
+
+       ds->ds_nbufs = nbufs;
+       if (nbufs > 0) {
+               ds->ds_buf = kmem_alloc(sizeof(struct dtv_buffer *) * nbufs,
+                   KM_SLEEP);
+               if (ds->ds_buf == NULL) {
+                       ds->ds_nbufs = oldnbufs;
+                       ds->ds_buf = oldbuf;
+                       return ENOMEM;
+               }
+       } else {
+               ds->ds_buf = NULL;
+       }
+
+       minnbufs = min(nbufs, oldnbufs);
+       for (i = 0; i < minnbufs; i++)
+               ds->ds_buf[i] = oldbuf[i];
+       for (; i < nbufs; i++)
+               ds->ds_buf[i] = dtv_buffer_alloc();
+       for (; i < oldnbufs; i++) {
+               dtv_buffer_free(oldbuf[i]);
+               oldbuf[i] = NULL;
+       }
+       if (oldbuf != NULL)
+               kmem_free(oldbuf, sizeof(struct dtv_buffer *) * oldnbufs);
+
+       offset = 0;
+       for (i = 0; i < nbufs; i++) {
+               ds->ds_buf[i]->db_offset = offset;
+               ds->ds_buf[i]->db_bytesused = 0;
+               ds->ds_buf[i]->db_length = PAGE_SIZE;
+               offset += PAGE_SIZE;
+       }
+
+       return 0;
+}
+
+static struct dtv_buffer *
+dtv_stream_dequeue(struct dtv_stream *ds)
+{
+       struct dtv_buffer *db;
+
+       if (!SIMPLEQ_EMPTY(&ds->ds_egress)) {
+               db = SIMPLEQ_FIRST(&ds->ds_egress);
+               SIMPLEQ_REMOVE_HEAD(&ds->ds_egress, db_entries);
+               return db;
+       }
+
+       return NULL;
+}
+
+static void
+dtv_stream_enqueue(struct dtv_stream *ds, struct dtv_buffer *db)
+{
+       db->db_bytesused = 0;
+       SIMPLEQ_INSERT_TAIL(&ds->ds_ingress, db, db_entries);
+}
+
+int
+dtv_buffer_setup(struct dtv_softc *sc, size_t bufsize)
+{
+       struct dtv_stream *ds = &sc->sc_stream;
+       unsigned int i;
+       int error;
+
+       mutex_enter(&ds->ds_lock);
+
+       error = dtv_buffer_realloc(sc, PAGE_ALIGN(bufsize));
+       if (error) {
+               mutex_exit(&ds->ds_lock);
+               return error;
+       }
+
+       for (i = 0; i < ds->ds_nbufs; i++)
+               dtv_stream_enqueue(ds, ds->ds_buf[i]);
+
+       mutex_exit(&ds->ds_lock);
+
+       return 0;
+}
+
+int
+dtv_buffer_destroy(struct dtv_softc *sc)
+{
+       struct dtv_stream *ds = &sc->sc_stream;
+
+       mutex_enter(&ds->ds_lock);
+
+       while (SIMPLEQ_FIRST(&ds->ds_ingress))
+               SIMPLEQ_REMOVE_HEAD(&ds->ds_ingress, db_entries);
+       while (SIMPLEQ_FIRST(&ds->ds_egress))
+               SIMPLEQ_REMOVE_HEAD(&ds->ds_egress, db_entries);
+       dtv_buffer_realloc(sc, 0);
+
+       mutex_exit(&ds->ds_lock);
+
+       return 0;
+}
+
+int
+dtv_buffer_read(struct dtv_softc *sc, struct uio *uio, int flags)
+{
+       struct dtv_stream *ds = &sc->sc_stream;
+       struct dtv_buffer *db;
+       struct dtv_scatter_io sio;
+       off_t offset;
+       size_t len, bread = 0;
+       int error;
+
+       while (uio->uio_resid > 0) {
+               mutex_enter(&ds->ds_lock);
+
+retry:
+               while (SIMPLEQ_EMPTY(&ds->ds_egress)) {
+                       if (flags & IO_NDELAY) {
+                               mutex_exit(&ds->ds_lock);
+                               return bread ? 0 : EWOULDBLOCK;
+                       }
+
+                       error = cv_wait_sig(&ds->ds_sample_cv, &ds->ds_lock);
+                       if (error) {
+                               mutex_exit(&ds->ds_lock);
+                               return EINTR;
+                       }
+               }
+               db = SIMPLEQ_FIRST(&ds->ds_egress);
+
+               if (db->db_bytesused == 0) {
+                       db = dtv_stream_dequeue(ds);
+                       dtv_stream_enqueue(ds, db);
+                       ds->ds_bytesread = 0;
+                       goto retry;
+               }
+
+               mutex_exit(&ds->ds_lock);
+
+               len = min(uio->uio_resid, db->db_bytesused - ds->ds_bytesread);
+               offset = db->db_offset + ds->ds_bytesread;
+
+               if (dtv_scatter_io_init(&ds->ds_data, offset, len, &sio)) {
+                       error = dtv_scatter_io_uiomove(&sio, uio);
+                       if (error == EFAULT)
+                               return EFAULT;
+                       ds->ds_bytesread += (len - sio.sio_resid);
+                       bread += (len - sio.sio_resid);
+               }
+



Home | Main Index | Thread Index | Old Index