summaryrefslogtreecommitdiff
path: root/sys/dev/dtv/dtv_buffer.c
blob: aa1d1136d96943bcd6233e272515c36fb7197802 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* $NetBSD: dtv_buffer.c,v 1.9 2018/09/03 16:29:30 riastradh 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_buffer.c,v 1.9 2018/09/03 16:29:30 riastradh 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	BLOCK_SIZE	DTV_DEFAULT_BLOCKSIZE
#define	BLOCK_ALIGN(a)	(((a) + BLOCK_SIZE - 1) & ~(BLOCK_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_ingress_lock);

		if (SIMPLEQ_EMPTY(&ds->ds_ingress)) {
			aprint_debug_dev(sc->sc_dev,
			    "dropping sample (%zu)\n", resid);
			mutex_exit(&ds->ds_ingress_lock);
			return;
		}

		db = SIMPLEQ_FIRST(&ds->ds_ingress);
		mutex_exit(&ds->ds_ingress_lock);

		avail = uimin(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) {
			mutex_enter(&ds->ds_ingress_lock);
			SIMPLEQ_REMOVE_HEAD(&ds->ds_ingress, db_entries);
			mutex_exit(&ds->ds_ingress_lock);
			mutex_enter(&ds->ds_egress_lock);
			SIMPLEQ_INSERT_TAIL(&ds->ds_egress, db, db_entries);
			selnotify(&ds->ds_sel, 0, 0);
			cv_broadcast(&ds->ds_sample_cv);
			mutex_exit(&ds->ds_egress_lock);
		}
	}
}

void
dtv_buffer_submit(void *priv, const struct dtv_payload *payload)
{
	struct dtv_softc *sc = priv;
	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)) {
			if (ts->ts_pidfilter[TS_PID(tspkt)]) {
				dtv_buffer_write(sc, tspkt, TS_PKTLEN);
			}
			dtv_demux_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));
}

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 = BLOCK_ALIGN(bufsize) / BLOCK_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);
	} else {
		ds->ds_buf = NULL;
	}

	minnbufs = uimin(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 = BLOCK_SIZE;
		offset += BLOCK_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)
{
	struct dtv_stream *ds = &sc->sc_stream;
	unsigned int i;

	mutex_enter(&ds->ds_ingress_lock);
	for (i = 0; i < ds->ds_nbufs; i++)
		dtv_stream_enqueue(ds, ds->ds_buf[i]);
	mutex_exit(&ds->ds_ingress_lock);

	return 0;
}

int
dtv_buffer_destroy(struct dtv_softc *sc)
{
	struct dtv_stream *ds = &sc->sc_stream;

	mutex_enter(&ds->ds_ingress_lock);
	while (SIMPLEQ_FIRST(&ds->ds_ingress))
		SIMPLEQ_REMOVE_HEAD(&ds->ds_ingress, db_entries);
	mutex_exit(&ds->ds_ingress_lock);
	mutex_enter(&ds->ds_egress_lock);
	while (SIMPLEQ_FIRST(&ds->ds_egress))
		SIMPLEQ_REMOVE_HEAD(&ds->ds_egress, db_entries);
	mutex_exit(&ds->ds_egress_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) {
retry:
		mutex_enter(&ds->ds_egress_lock);
		while (SIMPLEQ_EMPTY(&ds->ds_egress)) {
			if (flags & IO_NDELAY) {
				mutex_exit(&ds->ds_egress_lock);
				return EWOULDBLOCK;
			}

			error = cv_wait_sig(&ds->ds_sample_cv,
			    &ds->ds_egress_lock);
			if (error) {
				mutex_exit(&ds->ds_egress_lock);
				return EINTR;
			}
		}
		db = SIMPLEQ_FIRST(&ds->ds_egress);
		mutex_exit(&ds->ds_egress_lock);

		if (db->db_bytesused == 0) {
			mutex_enter(&ds->ds_egress_lock);
			db = dtv_stream_dequeue(ds);
			mutex_exit(&ds->ds_egress_lock);
			mutex_enter(&ds->ds_ingress_lock);
			dtv_stream_enqueue(ds, db);
			mutex_exit(&ds->ds_ingress_lock);
			ds->ds_bytesread = 0;
			goto retry;
		}

		len = uimin(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);
		}

		if (ds->ds_bytesread >= db->db_bytesused) {
			mutex_enter(&ds->ds_egress_lock);
			db = dtv_stream_dequeue(ds);
			mutex_exit(&ds->ds_egress_lock);
			mutex_enter(&ds->ds_ingress_lock);
			dtv_stream_enqueue(ds, db);
			mutex_exit(&ds->ds_ingress_lock);

			ds->ds_bytesread = 0;
		}
	}

	return 0;
}

int
dtv_buffer_poll(struct dtv_softc *sc, int events, lwp_t *l)
{
	struct dtv_stream *ds = &sc->sc_stream;
	int revents = 0;
#ifdef DTV_BUFFER_DEBUG
	struct dtv_buffer *db;
	size_t bufsize = 0;
#endif

	mutex_enter(&ds->ds_egress_lock);
	if (!SIMPLEQ_EMPTY(&ds->ds_egress)) {
#ifdef DTV_BUFFER_DEBUG
		SIMPLEQ_FOREACH(db, &ds->ds_egress, db_entries)
			bufsize += db->db_bytesused;
#endif
		revents |= (POLLIN | POLLOUT | POLLPRI);
	} else {
		selrecord(l, &ds->ds_sel);
	}
	mutex_exit(&ds->ds_egress_lock);

#ifdef DTV_BUFFER_DEBUG
	device_printf(sc->sc_dev, "%s: bufsize=%zu\n", __func__, bufsize);
#endif

	return revents;
}