dtv_buffer.c revision 1.1 1 /* $NetBSD: dtv_buffer.c,v 1.1 2011/07/09 14:46:56 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.1 2011/07/09 14:46:56 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 (%u)\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 static 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, size_t bufsize)
200 {
201 struct dtv_stream *ds = &sc->sc_stream;
202 unsigned int i;
203 int error;
204
205 mutex_enter(&ds->ds_lock);
206
207 error = dtv_buffer_realloc(sc, PAGE_ALIGN(bufsize));
208 if (error) {
209 mutex_exit(&ds->ds_lock);
210 return error;
211 }
212
213 for (i = 0; i < ds->ds_nbufs; i++)
214 dtv_stream_enqueue(ds, ds->ds_buf[i]);
215
216 mutex_exit(&ds->ds_lock);
217
218 return 0;
219 }
220
221 int
222 dtv_buffer_destroy(struct dtv_softc *sc)
223 {
224 struct dtv_stream *ds = &sc->sc_stream;
225
226 mutex_enter(&ds->ds_lock);
227
228 while (SIMPLEQ_FIRST(&ds->ds_ingress))
229 SIMPLEQ_REMOVE_HEAD(&ds->ds_ingress, db_entries);
230 while (SIMPLEQ_FIRST(&ds->ds_egress))
231 SIMPLEQ_REMOVE_HEAD(&ds->ds_egress, db_entries);
232 dtv_buffer_realloc(sc, 0);
233
234 mutex_exit(&ds->ds_lock);
235
236 return 0;
237 }
238
239 int
240 dtv_buffer_read(struct dtv_softc *sc, struct uio *uio, int flags)
241 {
242 struct dtv_stream *ds = &sc->sc_stream;
243 struct dtv_buffer *db;
244 struct dtv_scatter_io sio;
245 off_t offset;
246 size_t len, bread = 0;
247 int error;
248
249 while (uio->uio_resid > 0) {
250 mutex_enter(&ds->ds_lock);
251
252 retry:
253 while (SIMPLEQ_EMPTY(&ds->ds_egress)) {
254 if (flags & IO_NDELAY) {
255 mutex_exit(&ds->ds_lock);
256 return bread ? 0 : EWOULDBLOCK;
257 }
258
259 error = cv_wait_sig(&ds->ds_sample_cv, &ds->ds_lock);
260 if (error) {
261 mutex_exit(&ds->ds_lock);
262 return EINTR;
263 }
264 }
265 db = SIMPLEQ_FIRST(&ds->ds_egress);
266
267 if (db->db_bytesused == 0) {
268 db = dtv_stream_dequeue(ds);
269 dtv_stream_enqueue(ds, db);
270 ds->ds_bytesread = 0;
271 goto retry;
272 }
273
274 mutex_exit(&ds->ds_lock);
275
276 len = min(uio->uio_resid, db->db_bytesused - ds->ds_bytesread);
277 offset = db->db_offset + ds->ds_bytesread;
278
279 if (dtv_scatter_io_init(&ds->ds_data, offset, len, &sio)) {
280 error = dtv_scatter_io_uiomove(&sio, uio);
281 if (error == EFAULT)
282 return EFAULT;
283 ds->ds_bytesread += (len - sio.sio_resid);
284 bread += (len - sio.sio_resid);
285 }
286
287 if (ds->ds_bytesread >= db->db_bytesused) {
288 mutex_enter(&ds->ds_lock);
289 db = dtv_stream_dequeue(ds);
290 dtv_stream_enqueue(ds, db);
291 mutex_exit(&ds->ds_lock);
292
293 ds->ds_bytesread = 0;
294 }
295 }
296
297 return 0;
298 }
299
300 int
301 dtv_buffer_poll(struct dtv_softc *sc, int events, lwp_t *l)
302 {
303 struct dtv_stream *ds = &sc->sc_stream;
304 int revents = 0;
305
306 mutex_enter(&ds->ds_lock);
307 if (!SIMPLEQ_EMPTY(&ds->ds_egress)) {
308 revents |= (POLLIN | POLLOUT | POLLPRI);
309 } else {
310 selrecord(l, &ds->ds_sel);
311 }
312 mutex_exit(&ds->ds_lock);
313
314 return revents;
315 }
316