Home | History | Annotate | Line # | Download | only in dtv
dtv_buffer.c revision 1.3
      1 /* $NetBSD: dtv_buffer.c,v 1.3 2011/07/09 21:08:40 jmcneill 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_buffer.c,v 1.3 2011/07/09 21:08:40 jmcneill Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/kernel.h>
     40 #include <sys/types.h>
     41 #include <sys/conf.h>
     42 #include <sys/device.h>
     43 #include <sys/vnode.h>
     44 #include <sys/poll.h>
     45 #include <sys/select.h>
     46 
     47 #include <dev/dtv/dtvvar.h>
     48 
     49 #define	PAGE_ALIGN(a)	(((a) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))
     50 
     51 static void
     52 dtv_buffer_write(struct dtv_softc *sc, const uint8_t *buf, size_t buflen)
     53 {
     54 	struct dtv_stream *ds = &sc->sc_stream;
     55 	struct dtv_buffer *db;
     56 	struct dtv_scatter_io sio;
     57 	size_t resid = buflen, avail;
     58        	off_t offset = 0;
     59 
     60 	KASSERT(buflen == TS_PKTLEN);
     61 
     62 	while (resid > 0) {
     63 		mutex_enter(&ds->ds_lock);
     64 
     65 		if (SIMPLEQ_EMPTY(&ds->ds_ingress)) {
     66 			aprint_debug_dev(sc->sc_dev,
     67 			    "dropping sample (%zu)\n", resid);
     68 			mutex_exit(&ds->ds_lock);
     69 			return;
     70 		}
     71 
     72 		db = SIMPLEQ_FIRST(&ds->ds_ingress);
     73 		avail = min(db->db_length - db->db_bytesused, resid);
     74 		if (dtv_scatter_io_init(&ds->ds_data,
     75 		    db->db_offset + db->db_bytesused, avail, &sio)) {
     76 			dtv_scatter_io_copyin(&sio, buf + offset);
     77 			db->db_bytesused += (avail - sio.sio_resid);
     78 			offset += (avail - sio.sio_resid);
     79 			resid -= (avail - sio.sio_resid);
     80 		}
     81 
     82 		if (db->db_bytesused == db->db_length) {
     83 			SIMPLEQ_REMOVE_HEAD(&ds->ds_ingress, db_entries);
     84 			SIMPLEQ_INSERT_TAIL(&ds->ds_egress, db, db_entries);
     85 			cv_broadcast(&ds->ds_sample_cv);
     86 			selnotify(&ds->ds_sel, 0, 0);
     87 		}
     88 
     89 		mutex_exit(&ds->ds_lock);
     90 	}
     91 }
     92 
     93 void
     94 dtv_submit_payload(device_t self, const struct dtv_payload *payload)
     95 {
     96 	struct dtv_softc *sc = device_private(self);
     97 	struct dtv_ts *ts = &sc->sc_ts;
     98 	const uint8_t *tspkt;
     99 	unsigned int npkts, i;
    100 
    101 	tspkt = payload->data;
    102 	npkts = payload->size / TS_PKTLEN;
    103 	for (i = 0; i < npkts; i++) {
    104 		if (TS_HAS_SYNC(tspkt) && ts->ts_pidfilter[TS_PID(tspkt)]) {
    105 			dtv_buffer_write(sc, tspkt, TS_PKTLEN);
    106 		}
    107 		tspkt += TS_PKTLEN;
    108 	}
    109 }
    110 
    111 static struct dtv_buffer *
    112 dtv_buffer_alloc(void)
    113 {
    114 	return kmem_alloc(sizeof(struct dtv_buffer), KM_SLEEP);
    115 }
    116 
    117 static void
    118 dtv_buffer_free(struct dtv_buffer *db)
    119 {
    120 	kmem_free(db, sizeof(*db));
    121 }
    122 
    123 int
    124 dtv_buffer_realloc(struct dtv_softc *sc, size_t bufsize)
    125 {
    126 	struct dtv_stream *ds = &sc->sc_stream;
    127 	unsigned int i, nbufs, oldnbufs, minnbufs;
    128 	struct dtv_buffer **oldbuf;
    129 	off_t offset;
    130 	int error;
    131 
    132 	nbufs = PAGE_ALIGN(bufsize) / PAGE_SIZE;
    133 
    134 	error = dtv_scatter_buf_set_size(&ds->ds_data, bufsize);
    135 	if (error)
    136 		return error;
    137 
    138 	oldnbufs = ds->ds_nbufs;
    139 	oldbuf = ds->ds_buf;
    140 
    141 	ds->ds_nbufs = nbufs;
    142 	if (nbufs > 0) {
    143 		ds->ds_buf = kmem_alloc(sizeof(struct dtv_buffer *) * nbufs,
    144 		    KM_SLEEP);
    145 		if (ds->ds_buf == NULL) {
    146 			ds->ds_nbufs = oldnbufs;
    147 			ds->ds_buf = oldbuf;
    148 			return ENOMEM;
    149 		}
    150 	} else {
    151 		ds->ds_buf = NULL;
    152 	}
    153 
    154 	minnbufs = min(nbufs, oldnbufs);
    155 	for (i = 0; i < minnbufs; i++)
    156 		ds->ds_buf[i] = oldbuf[i];
    157 	for (; i < nbufs; i++)
    158 		ds->ds_buf[i] = dtv_buffer_alloc();
    159 	for (; i < oldnbufs; i++) {
    160 		dtv_buffer_free(oldbuf[i]);
    161 		oldbuf[i] = NULL;
    162 	}
    163 	if (oldbuf != NULL)
    164 		kmem_free(oldbuf, sizeof(struct dtv_buffer *) * oldnbufs);
    165 
    166 	offset = 0;
    167 	for (i = 0; i < nbufs; i++) {
    168 		ds->ds_buf[i]->db_offset = offset;
    169 		ds->ds_buf[i]->db_bytesused = 0;
    170 		ds->ds_buf[i]->db_length = PAGE_SIZE;
    171 		offset += PAGE_SIZE;
    172 	}
    173 
    174 	return 0;
    175 }
    176 
    177 static struct dtv_buffer *
    178 dtv_stream_dequeue(struct dtv_stream *ds)
    179 {
    180 	struct dtv_buffer *db;
    181 
    182 	if (!SIMPLEQ_EMPTY(&ds->ds_egress)) {
    183 		db = SIMPLEQ_FIRST(&ds->ds_egress);
    184 		SIMPLEQ_REMOVE_HEAD(&ds->ds_egress, db_entries);
    185 		return db;
    186 	}
    187 
    188 	return NULL;
    189 }
    190 
    191 static void
    192 dtv_stream_enqueue(struct dtv_stream *ds, struct dtv_buffer *db)
    193 {
    194 	db->db_bytesused = 0;
    195 	SIMPLEQ_INSERT_TAIL(&ds->ds_ingress, db, db_entries);
    196 }
    197 
    198 int
    199 dtv_buffer_setup(struct dtv_softc *sc)
    200 {
    201 	struct dtv_stream *ds = &sc->sc_stream;
    202 	unsigned int i;
    203 
    204 	mutex_enter(&ds->ds_lock);
    205 	for (i = 0; i < ds->ds_nbufs; i++)
    206 		dtv_stream_enqueue(ds, ds->ds_buf[i]);
    207 	mutex_exit(&ds->ds_lock);
    208 
    209 	return 0;
    210 }
    211 
    212 int
    213 dtv_buffer_destroy(struct dtv_softc *sc)
    214 {
    215 	struct dtv_stream *ds = &sc->sc_stream;
    216 
    217 	mutex_enter(&ds->ds_lock);
    218 	while (SIMPLEQ_FIRST(&ds->ds_ingress))
    219 		SIMPLEQ_REMOVE_HEAD(&ds->ds_ingress, db_entries);
    220 	while (SIMPLEQ_FIRST(&ds->ds_egress))
    221 		SIMPLEQ_REMOVE_HEAD(&ds->ds_egress, db_entries);
    222 	mutex_exit(&ds->ds_lock);
    223 
    224 	return 0;
    225 }
    226 
    227 int
    228 dtv_buffer_read(struct dtv_softc *sc, struct uio *uio, int flags)
    229 {
    230 	struct dtv_stream *ds = &sc->sc_stream;
    231 	struct dtv_buffer *db;
    232 	struct dtv_scatter_io sio;
    233 	off_t offset;
    234 	size_t len, bread = 0;
    235 	int error;
    236 
    237 	while (uio->uio_resid > 0) {
    238 		mutex_enter(&ds->ds_lock);
    239 
    240 retry:
    241 		while (SIMPLEQ_EMPTY(&ds->ds_egress)) {
    242 			if (flags & IO_NDELAY) {
    243 				mutex_exit(&ds->ds_lock);
    244 				return bread ? 0 : EWOULDBLOCK;
    245 			}
    246 
    247 			error = cv_wait_sig(&ds->ds_sample_cv, &ds->ds_lock);
    248 			if (error) {
    249 				mutex_exit(&ds->ds_lock);
    250 				return EINTR;
    251 			}
    252 		}
    253 		db = SIMPLEQ_FIRST(&ds->ds_egress);
    254 
    255 		if (db->db_bytesused == 0) {
    256 			db = dtv_stream_dequeue(ds);
    257 			dtv_stream_enqueue(ds, db);
    258 			ds->ds_bytesread = 0;
    259 			goto retry;
    260 		}
    261 
    262 		mutex_exit(&ds->ds_lock);
    263 
    264 		len = min(uio->uio_resid, db->db_bytesused - ds->ds_bytesread);
    265 		offset = db->db_offset + ds->ds_bytesread;
    266 
    267 		if (dtv_scatter_io_init(&ds->ds_data, offset, len, &sio)) {
    268 			error = dtv_scatter_io_uiomove(&sio, uio);
    269 			if (error == EFAULT)
    270 				return EFAULT;
    271 			ds->ds_bytesread += (len - sio.sio_resid);
    272 			bread += (len - sio.sio_resid);
    273 		}
    274 
    275 		if (ds->ds_bytesread >= db->db_bytesused) {
    276 			mutex_enter(&ds->ds_lock);
    277 			db = dtv_stream_dequeue(ds);
    278 			dtv_stream_enqueue(ds, db);
    279 			mutex_exit(&ds->ds_lock);
    280 
    281 			ds->ds_bytesread = 0;
    282 		}
    283 	}
    284 
    285 	return 0;
    286 }
    287 
    288 int
    289 dtv_buffer_poll(struct dtv_softc *sc, int events, lwp_t *l)
    290 {
    291 	struct dtv_stream *ds = &sc->sc_stream;
    292 	int revents = 0;
    293 
    294 	mutex_enter(&ds->ds_lock);
    295 	if (!SIMPLEQ_EMPTY(&ds->ds_egress)) {
    296 		revents |= (POLLIN | POLLOUT | POLLPRI);
    297 	} else {
    298 		selrecord(l, &ds->ds_sel);
    299 	}
    300 	mutex_exit(&ds->ds_lock);
    301 
    302 	return revents;
    303 }
    304