dtv_buffer.c revision 1.5 1 1.5 jmcneill /* $NetBSD: dtv_buffer.c,v 1.5 2011/07/13 22:43:04 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2011 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill * 3. All advertising materials mentioning features or use of this software
16 1.1 jmcneill * must display the following acknowledgement:
17 1.1 jmcneill * This product includes software developed by Jared D. McNeill.
18 1.1 jmcneill * 4. Neither the name of The NetBSD Foundation nor the names of its
19 1.1 jmcneill * contributors may be used to endorse or promote products derived
20 1.1 jmcneill * from this software without specific prior written permission.
21 1.1 jmcneill *
22 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE.
33 1.1 jmcneill */
34 1.1 jmcneill
35 1.1 jmcneill #include <sys/cdefs.h>
36 1.5 jmcneill __KERNEL_RCSID(0, "$NetBSD: dtv_buffer.c,v 1.5 2011/07/13 22:43:04 jmcneill Exp $");
37 1.1 jmcneill
38 1.1 jmcneill #include <sys/param.h>
39 1.1 jmcneill #include <sys/kernel.h>
40 1.1 jmcneill #include <sys/types.h>
41 1.1 jmcneill #include <sys/conf.h>
42 1.1 jmcneill #include <sys/device.h>
43 1.1 jmcneill #include <sys/vnode.h>
44 1.1 jmcneill #include <sys/poll.h>
45 1.1 jmcneill #include <sys/select.h>
46 1.1 jmcneill
47 1.1 jmcneill #include <dev/dtv/dtvvar.h>
48 1.1 jmcneill
49 1.4 jmcneill #define BLOCK_SIZE DTV_DEFAULT_BLOCKSIZE
50 1.4 jmcneill #define BLOCK_ALIGN(a) (((a) + BLOCK_SIZE - 1) & ~(BLOCK_SIZE - 1))
51 1.1 jmcneill
52 1.1 jmcneill static void
53 1.1 jmcneill dtv_buffer_write(struct dtv_softc *sc, const uint8_t *buf, size_t buflen)
54 1.1 jmcneill {
55 1.1 jmcneill struct dtv_stream *ds = &sc->sc_stream;
56 1.1 jmcneill struct dtv_buffer *db;
57 1.1 jmcneill struct dtv_scatter_io sio;
58 1.1 jmcneill size_t resid = buflen, avail;
59 1.1 jmcneill off_t offset = 0;
60 1.1 jmcneill
61 1.1 jmcneill KASSERT(buflen == TS_PKTLEN);
62 1.1 jmcneill
63 1.1 jmcneill while (resid > 0) {
64 1.4 jmcneill mutex_enter(&ds->ds_ingress_lock);
65 1.1 jmcneill
66 1.1 jmcneill if (SIMPLEQ_EMPTY(&ds->ds_ingress)) {
67 1.1 jmcneill aprint_debug_dev(sc->sc_dev,
68 1.3 jmcneill "dropping sample (%zu)\n", resid);
69 1.4 jmcneill mutex_exit(&ds->ds_ingress_lock);
70 1.1 jmcneill return;
71 1.1 jmcneill }
72 1.1 jmcneill
73 1.1 jmcneill db = SIMPLEQ_FIRST(&ds->ds_ingress);
74 1.4 jmcneill mutex_exit(&ds->ds_ingress_lock);
75 1.4 jmcneill
76 1.1 jmcneill avail = min(db->db_length - db->db_bytesused, resid);
77 1.1 jmcneill if (dtv_scatter_io_init(&ds->ds_data,
78 1.1 jmcneill db->db_offset + db->db_bytesused, avail, &sio)) {
79 1.1 jmcneill dtv_scatter_io_copyin(&sio, buf + offset);
80 1.1 jmcneill db->db_bytesused += (avail - sio.sio_resid);
81 1.1 jmcneill offset += (avail - sio.sio_resid);
82 1.1 jmcneill resid -= (avail - sio.sio_resid);
83 1.1 jmcneill }
84 1.1 jmcneill
85 1.1 jmcneill if (db->db_bytesused == db->db_length) {
86 1.4 jmcneill mutex_enter(&ds->ds_ingress_lock);
87 1.1 jmcneill SIMPLEQ_REMOVE_HEAD(&ds->ds_ingress, db_entries);
88 1.4 jmcneill mutex_exit(&ds->ds_ingress_lock);
89 1.4 jmcneill mutex_enter(&ds->ds_egress_lock);
90 1.1 jmcneill SIMPLEQ_INSERT_TAIL(&ds->ds_egress, db, db_entries);
91 1.4 jmcneill selnotify(&ds->ds_sel, 0, 0);
92 1.1 jmcneill cv_broadcast(&ds->ds_sample_cv);
93 1.4 jmcneill mutex_exit(&ds->ds_egress_lock);
94 1.1 jmcneill }
95 1.1 jmcneill }
96 1.1 jmcneill }
97 1.1 jmcneill
98 1.1 jmcneill void
99 1.1 jmcneill dtv_submit_payload(device_t self, const struct dtv_payload *payload)
100 1.1 jmcneill {
101 1.1 jmcneill struct dtv_softc *sc = device_private(self);
102 1.5 jmcneill struct dtv_demux *demux;
103 1.1 jmcneill struct dtv_ts *ts = &sc->sc_ts;
104 1.1 jmcneill const uint8_t *tspkt;
105 1.1 jmcneill unsigned int npkts, i;
106 1.1 jmcneill
107 1.1 jmcneill tspkt = payload->data;
108 1.1 jmcneill npkts = payload->size / TS_PKTLEN;
109 1.1 jmcneill for (i = 0; i < npkts; i++) {
110 1.5 jmcneill if (TS_HAS_SYNC(tspkt)) {
111 1.5 jmcneill if (ts->ts_pidfilter[TS_PID(tspkt)]) {
112 1.5 jmcneill dtv_buffer_write(sc, tspkt, TS_PKTLEN);
113 1.5 jmcneill }
114 1.5 jmcneill mutex_enter(&sc->sc_demux_lock);
115 1.5 jmcneill TAILQ_FOREACH(demux, &sc->sc_demux_list, dd_entries) {
116 1.5 jmcneill dtv_demux_write(demux, tspkt, TS_PKTLEN);
117 1.5 jmcneill }
118 1.5 jmcneill mutex_exit(&sc->sc_demux_lock);
119 1.1 jmcneill }
120 1.1 jmcneill tspkt += TS_PKTLEN;
121 1.1 jmcneill }
122 1.1 jmcneill }
123 1.1 jmcneill
124 1.1 jmcneill static struct dtv_buffer *
125 1.1 jmcneill dtv_buffer_alloc(void)
126 1.1 jmcneill {
127 1.1 jmcneill return kmem_alloc(sizeof(struct dtv_buffer), KM_SLEEP);
128 1.1 jmcneill }
129 1.1 jmcneill
130 1.1 jmcneill static void
131 1.1 jmcneill dtv_buffer_free(struct dtv_buffer *db)
132 1.1 jmcneill {
133 1.1 jmcneill kmem_free(db, sizeof(*db));
134 1.1 jmcneill }
135 1.1 jmcneill
136 1.2 jmcneill int
137 1.1 jmcneill dtv_buffer_realloc(struct dtv_softc *sc, size_t bufsize)
138 1.1 jmcneill {
139 1.1 jmcneill struct dtv_stream *ds = &sc->sc_stream;
140 1.1 jmcneill unsigned int i, nbufs, oldnbufs, minnbufs;
141 1.1 jmcneill struct dtv_buffer **oldbuf;
142 1.1 jmcneill off_t offset;
143 1.1 jmcneill int error;
144 1.1 jmcneill
145 1.4 jmcneill nbufs = BLOCK_ALIGN(bufsize) / BLOCK_SIZE;
146 1.1 jmcneill
147 1.1 jmcneill error = dtv_scatter_buf_set_size(&ds->ds_data, bufsize);
148 1.1 jmcneill if (error)
149 1.1 jmcneill return error;
150 1.1 jmcneill
151 1.1 jmcneill oldnbufs = ds->ds_nbufs;
152 1.1 jmcneill oldbuf = ds->ds_buf;
153 1.1 jmcneill
154 1.1 jmcneill ds->ds_nbufs = nbufs;
155 1.1 jmcneill if (nbufs > 0) {
156 1.1 jmcneill ds->ds_buf = kmem_alloc(sizeof(struct dtv_buffer *) * nbufs,
157 1.1 jmcneill KM_SLEEP);
158 1.1 jmcneill if (ds->ds_buf == NULL) {
159 1.1 jmcneill ds->ds_nbufs = oldnbufs;
160 1.1 jmcneill ds->ds_buf = oldbuf;
161 1.1 jmcneill return ENOMEM;
162 1.1 jmcneill }
163 1.1 jmcneill } else {
164 1.1 jmcneill ds->ds_buf = NULL;
165 1.1 jmcneill }
166 1.1 jmcneill
167 1.1 jmcneill minnbufs = min(nbufs, oldnbufs);
168 1.1 jmcneill for (i = 0; i < minnbufs; i++)
169 1.1 jmcneill ds->ds_buf[i] = oldbuf[i];
170 1.1 jmcneill for (; i < nbufs; i++)
171 1.1 jmcneill ds->ds_buf[i] = dtv_buffer_alloc();
172 1.1 jmcneill for (; i < oldnbufs; i++) {
173 1.1 jmcneill dtv_buffer_free(oldbuf[i]);
174 1.1 jmcneill oldbuf[i] = NULL;
175 1.1 jmcneill }
176 1.1 jmcneill if (oldbuf != NULL)
177 1.1 jmcneill kmem_free(oldbuf, sizeof(struct dtv_buffer *) * oldnbufs);
178 1.1 jmcneill
179 1.1 jmcneill offset = 0;
180 1.1 jmcneill for (i = 0; i < nbufs; i++) {
181 1.1 jmcneill ds->ds_buf[i]->db_offset = offset;
182 1.1 jmcneill ds->ds_buf[i]->db_bytesused = 0;
183 1.4 jmcneill ds->ds_buf[i]->db_length = BLOCK_SIZE;
184 1.4 jmcneill offset += BLOCK_SIZE;
185 1.1 jmcneill }
186 1.1 jmcneill
187 1.1 jmcneill return 0;
188 1.1 jmcneill }
189 1.1 jmcneill
190 1.1 jmcneill static struct dtv_buffer *
191 1.1 jmcneill dtv_stream_dequeue(struct dtv_stream *ds)
192 1.1 jmcneill {
193 1.1 jmcneill struct dtv_buffer *db;
194 1.1 jmcneill
195 1.1 jmcneill if (!SIMPLEQ_EMPTY(&ds->ds_egress)) {
196 1.1 jmcneill db = SIMPLEQ_FIRST(&ds->ds_egress);
197 1.1 jmcneill SIMPLEQ_REMOVE_HEAD(&ds->ds_egress, db_entries);
198 1.1 jmcneill return db;
199 1.1 jmcneill }
200 1.1 jmcneill
201 1.1 jmcneill return NULL;
202 1.1 jmcneill }
203 1.1 jmcneill
204 1.1 jmcneill static void
205 1.1 jmcneill dtv_stream_enqueue(struct dtv_stream *ds, struct dtv_buffer *db)
206 1.1 jmcneill {
207 1.1 jmcneill db->db_bytesused = 0;
208 1.1 jmcneill SIMPLEQ_INSERT_TAIL(&ds->ds_ingress, db, db_entries);
209 1.1 jmcneill }
210 1.1 jmcneill
211 1.1 jmcneill int
212 1.2 jmcneill dtv_buffer_setup(struct dtv_softc *sc)
213 1.1 jmcneill {
214 1.1 jmcneill struct dtv_stream *ds = &sc->sc_stream;
215 1.1 jmcneill unsigned int i;
216 1.1 jmcneill
217 1.4 jmcneill mutex_enter(&ds->ds_ingress_lock);
218 1.1 jmcneill for (i = 0; i < ds->ds_nbufs; i++)
219 1.1 jmcneill dtv_stream_enqueue(ds, ds->ds_buf[i]);
220 1.4 jmcneill mutex_exit(&ds->ds_ingress_lock);
221 1.1 jmcneill
222 1.1 jmcneill return 0;
223 1.1 jmcneill }
224 1.1 jmcneill
225 1.1 jmcneill int
226 1.1 jmcneill dtv_buffer_destroy(struct dtv_softc *sc)
227 1.1 jmcneill {
228 1.1 jmcneill struct dtv_stream *ds = &sc->sc_stream;
229 1.1 jmcneill
230 1.4 jmcneill mutex_enter(&ds->ds_ingress_lock);
231 1.1 jmcneill while (SIMPLEQ_FIRST(&ds->ds_ingress))
232 1.1 jmcneill SIMPLEQ_REMOVE_HEAD(&ds->ds_ingress, db_entries);
233 1.4 jmcneill mutex_exit(&ds->ds_ingress_lock);
234 1.4 jmcneill mutex_enter(&ds->ds_egress_lock);
235 1.1 jmcneill while (SIMPLEQ_FIRST(&ds->ds_egress))
236 1.1 jmcneill SIMPLEQ_REMOVE_HEAD(&ds->ds_egress, db_entries);
237 1.4 jmcneill mutex_exit(&ds->ds_egress_lock);
238 1.1 jmcneill
239 1.1 jmcneill return 0;
240 1.1 jmcneill }
241 1.1 jmcneill
242 1.1 jmcneill int
243 1.1 jmcneill dtv_buffer_read(struct dtv_softc *sc, struct uio *uio, int flags)
244 1.1 jmcneill {
245 1.1 jmcneill struct dtv_stream *ds = &sc->sc_stream;
246 1.1 jmcneill struct dtv_buffer *db;
247 1.1 jmcneill struct dtv_scatter_io sio;
248 1.1 jmcneill off_t offset;
249 1.1 jmcneill size_t len, bread = 0;
250 1.1 jmcneill int error;
251 1.1 jmcneill
252 1.1 jmcneill while (uio->uio_resid > 0) {
253 1.1 jmcneill retry:
254 1.4 jmcneill mutex_enter(&ds->ds_egress_lock);
255 1.1 jmcneill while (SIMPLEQ_EMPTY(&ds->ds_egress)) {
256 1.1 jmcneill if (flags & IO_NDELAY) {
257 1.4 jmcneill mutex_exit(&ds->ds_egress_lock);
258 1.4 jmcneill return EWOULDBLOCK;
259 1.1 jmcneill }
260 1.1 jmcneill
261 1.4 jmcneill error = cv_wait_sig(&ds->ds_sample_cv,
262 1.4 jmcneill &ds->ds_egress_lock);
263 1.1 jmcneill if (error) {
264 1.4 jmcneill mutex_exit(&ds->ds_egress_lock);
265 1.1 jmcneill return EINTR;
266 1.1 jmcneill }
267 1.1 jmcneill }
268 1.1 jmcneill db = SIMPLEQ_FIRST(&ds->ds_egress);
269 1.4 jmcneill mutex_exit(&ds->ds_egress_lock);
270 1.1 jmcneill
271 1.1 jmcneill if (db->db_bytesused == 0) {
272 1.4 jmcneill mutex_enter(&ds->ds_egress_lock);
273 1.1 jmcneill db = dtv_stream_dequeue(ds);
274 1.4 jmcneill mutex_exit(&ds->ds_egress_lock);
275 1.4 jmcneill mutex_enter(&ds->ds_ingress_lock);
276 1.1 jmcneill dtv_stream_enqueue(ds, db);
277 1.4 jmcneill mutex_exit(&ds->ds_ingress_lock);
278 1.1 jmcneill ds->ds_bytesread = 0;
279 1.1 jmcneill goto retry;
280 1.1 jmcneill }
281 1.1 jmcneill
282 1.1 jmcneill len = min(uio->uio_resid, db->db_bytesused - ds->ds_bytesread);
283 1.1 jmcneill offset = db->db_offset + ds->ds_bytesread;
284 1.1 jmcneill
285 1.1 jmcneill if (dtv_scatter_io_init(&ds->ds_data, offset, len, &sio)) {
286 1.1 jmcneill error = dtv_scatter_io_uiomove(&sio, uio);
287 1.1 jmcneill if (error == EFAULT)
288 1.1 jmcneill return EFAULT;
289 1.1 jmcneill ds->ds_bytesread += (len - sio.sio_resid);
290 1.1 jmcneill bread += (len - sio.sio_resid);
291 1.1 jmcneill }
292 1.1 jmcneill
293 1.1 jmcneill if (ds->ds_bytesread >= db->db_bytesused) {
294 1.4 jmcneill mutex_enter(&ds->ds_egress_lock);
295 1.1 jmcneill db = dtv_stream_dequeue(ds);
296 1.4 jmcneill mutex_exit(&ds->ds_egress_lock);
297 1.4 jmcneill mutex_enter(&ds->ds_ingress_lock);
298 1.1 jmcneill dtv_stream_enqueue(ds, db);
299 1.4 jmcneill mutex_exit(&ds->ds_ingress_lock);
300 1.1 jmcneill
301 1.1 jmcneill ds->ds_bytesread = 0;
302 1.1 jmcneill }
303 1.1 jmcneill }
304 1.1 jmcneill
305 1.1 jmcneill return 0;
306 1.1 jmcneill }
307 1.1 jmcneill
308 1.1 jmcneill int
309 1.1 jmcneill dtv_buffer_poll(struct dtv_softc *sc, int events, lwp_t *l)
310 1.1 jmcneill {
311 1.1 jmcneill struct dtv_stream *ds = &sc->sc_stream;
312 1.1 jmcneill int revents = 0;
313 1.4 jmcneill #ifdef DTV_BUFFER_DEBUG
314 1.4 jmcneill struct dtv_buffer *db;
315 1.4 jmcneill size_t bufsize = 0;
316 1.4 jmcneill #endif
317 1.1 jmcneill
318 1.4 jmcneill mutex_enter(&ds->ds_egress_lock);
319 1.1 jmcneill if (!SIMPLEQ_EMPTY(&ds->ds_egress)) {
320 1.4 jmcneill #ifdef DTV_BUFFER_DEBUG
321 1.4 jmcneill SIMPLEQ_FOREACH(db, &ds->ds_egress, db_entries)
322 1.4 jmcneill bufsize += db->db_bytesused;
323 1.4 jmcneill #endif
324 1.1 jmcneill revents |= (POLLIN | POLLOUT | POLLPRI);
325 1.1 jmcneill } else {
326 1.1 jmcneill selrecord(l, &ds->ds_sel);
327 1.1 jmcneill }
328 1.4 jmcneill mutex_exit(&ds->ds_egress_lock);
329 1.4 jmcneill
330 1.4 jmcneill #ifdef DTV_BUFFER_DEBUG
331 1.4 jmcneill device_printf(sc->sc_dev, "%s: bufsize=%zu\n", __func__, bufsize);
332 1.4 jmcneill #endif
333 1.1 jmcneill
334 1.1 jmcneill return revents;
335 1.1 jmcneill }
336