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