4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "pvrusb2-ioread.h"
22 #include "pvrusb2-debug.h"
23 #include <linux/errno.h>
24 #include <linux/string.h>
25 #include <linux/slab.h>
26 #include <linux/mutex.h>
27 #include <asm/uaccess.h>
29 #define BUFFER_COUNT 32
30 #define BUFFER_SIZE PAGE_ALIGN(0x4000)
33 struct pvr2_stream *stream;
34 char *buffer_storage[BUFFER_COUNT];
36 unsigned int sync_key_len;
37 unsigned int sync_buf_offs;
38 unsigned int sync_state;
39 unsigned int sync_trashed_count;
40 int enabled; // Streaming is on
41 int spigot_open; // OK to pass data to client
42 int stream_running; // Passing data to client now
44 /* State relevant to current buffer being read */
45 struct pvr2_buffer *c_buf;
47 unsigned int c_data_len;
48 unsigned int c_data_offs;
52 static int pvr2_ioread_init(struct pvr2_ioread *cp)
57 mutex_init(&cp->mutex);
59 for (idx = 0; idx < BUFFER_COUNT; idx++) {
60 cp->buffer_storage[idx] = kmalloc(BUFFER_SIZE,GFP_KERNEL);
61 if (!(cp->buffer_storage[idx])) break;
64 if (idx < BUFFER_COUNT) {
65 // An allocation appears to have failed
66 for (idx = 0; idx < BUFFER_COUNT; idx++) {
67 if (!(cp->buffer_storage[idx])) continue;
68 kfree(cp->buffer_storage[idx]);
75 static void pvr2_ioread_done(struct pvr2_ioread *cp)
79 pvr2_ioread_setup(cp,NULL);
80 for (idx = 0; idx < BUFFER_COUNT; idx++) {
81 if (!(cp->buffer_storage[idx])) continue;
82 kfree(cp->buffer_storage[idx]);
86 struct pvr2_ioread *pvr2_ioread_create(void)
88 struct pvr2_ioread *cp;
89 cp = kzalloc(sizeof(*cp),GFP_KERNEL);
91 pvr2_trace(PVR2_TRACE_STRUCT,"pvr2_ioread_create id=%p",cp);
92 if (pvr2_ioread_init(cp) < 0) {
99 void pvr2_ioread_destroy(struct pvr2_ioread *cp)
102 pvr2_ioread_done(cp);
103 pvr2_trace(PVR2_TRACE_STRUCT,"pvr2_ioread_destroy id=%p",cp);
104 if (cp->sync_key_ptr) {
105 kfree(cp->sync_key_ptr);
106 cp->sync_key_ptr = NULL;
111 void pvr2_ioread_set_sync_key(struct pvr2_ioread *cp,
112 const char *sync_key_ptr,
113 unsigned int sync_key_len)
117 if (!sync_key_ptr) sync_key_len = 0;
118 if ((sync_key_len == cp->sync_key_len) &&
120 (!memcmp(sync_key_ptr,cp->sync_key_ptr,sync_key_len)))) return;
122 if (sync_key_len != cp->sync_key_len) {
123 if (cp->sync_key_ptr) {
124 kfree(cp->sync_key_ptr);
125 cp->sync_key_ptr = NULL;
127 cp->sync_key_len = 0;
129 cp->sync_key_ptr = kmalloc(sync_key_len,GFP_KERNEL);
130 if (cp->sync_key_ptr) {
131 cp->sync_key_len = sync_key_len;
135 if (!cp->sync_key_len) return;
136 memcpy(cp->sync_key_ptr,sync_key_ptr,cp->sync_key_len);
139 static void pvr2_ioread_stop(struct pvr2_ioread *cp)
141 if (!(cp->enabled)) return;
142 pvr2_trace(PVR2_TRACE_START_STOP,
143 "/*---TRACE_READ---*/ pvr2_ioread_stop id=%p",cp);
144 pvr2_stream_kill(cp->stream);
146 cp->c_data_ptr = NULL;
150 cp->stream_running = 0;
152 if (cp->sync_state) {
153 pvr2_trace(PVR2_TRACE_DATA_FLOW,
154 "/*---TRACE_READ---*/ sync_state <== 0");
159 static int pvr2_ioread_start(struct pvr2_ioread *cp)
162 struct pvr2_buffer *bp;
163 if (cp->enabled) return 0;
164 if (!(cp->stream)) return 0;
165 pvr2_trace(PVR2_TRACE_START_STOP,
166 "/*---TRACE_READ---*/ pvr2_ioread_start id=%p",cp);
167 while ((bp = pvr2_stream_get_idle_buffer(cp->stream)) != NULL) {
168 stat = pvr2_buffer_queue(bp);
170 pvr2_trace(PVR2_TRACE_DATA_FLOW,
171 "/*---TRACE_READ---*/"
172 " pvr2_ioread_start id=%p"
175 pvr2_ioread_stop(cp);
181 cp->c_data_ptr = NULL;
184 cp->stream_running = 0;
185 if (cp->sync_key_len) {
186 pvr2_trace(PVR2_TRACE_DATA_FLOW,
187 "/*---TRACE_READ---*/ sync_state <== 1");
189 cp->sync_trashed_count = 0;
190 cp->sync_buf_offs = 0;
196 struct pvr2_stream *pvr2_ioread_get_stream(struct pvr2_ioread *cp)
201 int pvr2_ioread_setup(struct pvr2_ioread *cp,struct pvr2_stream *sp)
205 struct pvr2_buffer *bp;
207 mutex_lock(&cp->mutex); do {
209 pvr2_trace(PVR2_TRACE_START_STOP,
210 "/*---TRACE_READ---*/"
211 " pvr2_ioread_setup (tear-down) id=%p",cp);
212 pvr2_ioread_stop(cp);
213 pvr2_stream_kill(cp->stream);
214 if (pvr2_stream_get_buffer_count(cp->stream)) {
215 pvr2_stream_set_buffer_count(cp->stream,0);
220 pvr2_trace(PVR2_TRACE_START_STOP,
221 "/*---TRACE_READ---*/"
222 " pvr2_ioread_setup (setup) id=%p",cp);
223 pvr2_stream_kill(sp);
224 ret = pvr2_stream_set_buffer_count(sp,BUFFER_COUNT);
225 if (ret < 0) return ret;
226 for (idx = 0; idx < BUFFER_COUNT; idx++) {
227 bp = pvr2_stream_get_buffer(sp,idx);
228 pvr2_buffer_set_buffer(bp,
229 cp->buffer_storage[idx],
234 } while (0); mutex_unlock(&cp->mutex);
239 int pvr2_ioread_set_enabled(struct pvr2_ioread *cp,int fl)
242 if ((!fl) == (!(cp->enabled))) return ret;
244 mutex_lock(&cp->mutex); do {
246 ret = pvr2_ioread_start(cp);
248 pvr2_ioread_stop(cp);
250 } while (0); mutex_unlock(&cp->mutex);
254 static int pvr2_ioread_get_buffer(struct pvr2_ioread *cp)
258 while (cp->c_data_len <= cp->c_data_offs) {
260 // Flush out current buffer first.
261 stat = pvr2_buffer_queue(cp->c_buf);
263 // Streaming error...
264 pvr2_trace(PVR2_TRACE_DATA_FLOW,
265 "/*---TRACE_READ---*/"
266 " pvr2_ioread_read id=%p"
269 pvr2_ioread_stop(cp);
273 cp->c_data_ptr = NULL;
277 // Now get a freshly filled buffer.
278 cp->c_buf = pvr2_stream_get_ready_buffer(cp->stream);
279 if (!cp->c_buf) break; // Nothing ready; done.
280 cp->c_data_len = pvr2_buffer_get_count(cp->c_buf);
281 if (!cp->c_data_len) {
282 // Nothing transferred. Was there an error?
283 stat = pvr2_buffer_get_status(cp->c_buf);
285 // Streaming error...
286 pvr2_trace(PVR2_TRACE_DATA_FLOW,
287 "/*---TRACE_READ---*/"
288 " pvr2_ioread_read id=%p"
291 pvr2_ioread_stop(cp);
299 cp->c_data_ptr = cp->buffer_storage[
300 pvr2_buffer_get_id(cp->c_buf)];
305 static void pvr2_ioread_filter(struct pvr2_ioread *cp)
308 if (!cp->enabled) return;
309 if (cp->sync_state != 1) return;
311 // Search the stream for our synchronization key. This is made
312 // complicated by the fact that in order to be honest with
313 // ourselves here we must search across buffer boundaries...
314 mutex_lock(&cp->mutex); while (1) {
315 // Ensure we have a buffer
316 if (!pvr2_ioread_get_buffer(cp)) break;
317 if (!cp->c_data_len) break;
319 // Now walk the buffer contents until we match the key or
320 // run out of buffer data.
321 for (idx = cp->c_data_offs; idx < cp->c_data_len; idx++) {
322 if (cp->sync_buf_offs >= cp->sync_key_len) break;
323 if (cp->c_data_ptr[idx] ==
324 cp->sync_key_ptr[cp->sync_buf_offs]) {
325 // Found the next key byte
326 (cp->sync_buf_offs)++;
328 // Whoops, mismatched. Start key over...
329 cp->sync_buf_offs = 0;
333 // Consume what we've walked through
334 cp->c_data_offs += idx;
335 cp->sync_trashed_count += idx;
337 // If we've found the key, then update state and get out.
338 if (cp->sync_buf_offs >= cp->sync_key_len) {
339 cp->sync_trashed_count -= cp->sync_key_len;
340 pvr2_trace(PVR2_TRACE_DATA_FLOW,
341 "/*---TRACE_READ---*/"
342 " sync_state <== 2 (skipped %u bytes)",
343 cp->sync_trashed_count);
345 cp->sync_buf_offs = 0;
349 if (cp->c_data_offs < cp->c_data_len) {
350 // Sanity check - should NEVER get here
351 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
352 "ERROR: pvr2_ioread filter sync problem"
354 cp->c_data_len,cp->c_data_offs);
355 // Get out so we don't get stuck in an infinite
360 continue; // (for clarity)
361 } mutex_unlock(&cp->mutex);
364 int pvr2_ioread_avail(struct pvr2_ioread *cp)
367 if (!(cp->enabled)) {
368 // Stream is not enabled; so this is an I/O error
372 if (cp->sync_state == 1) {
373 pvr2_ioread_filter(cp);
374 if (cp->sync_state == 1) return -EAGAIN;
378 if (cp->stream_running) {
379 if (!pvr2_stream_get_ready_count(cp->stream)) {
380 // No data available at all right now.
384 if (pvr2_stream_get_ready_count(cp->stream) < BUFFER_COUNT/2) {
385 // Haven't buffered up enough yet; try again later
390 if ((!(cp->spigot_open)) != (!(ret == 0))) {
391 cp->spigot_open = (ret == 0);
392 pvr2_trace(PVR2_TRACE_DATA_FLOW,
393 "/*---TRACE_READ---*/ data is %s",
394 cp->spigot_open ? "available" : "pending");
400 int pvr2_ioread_read(struct pvr2_ioread *cp,void __user *buf,unsigned int cnt)
402 unsigned int copied_cnt;
407 unsigned int req_cnt = cnt;
410 pvr2_trace(PVR2_TRACE_TRAP,
411 "/*---TRACE_READ---*/ pvr2_ioread_read id=%p"
412 " ZERO Request? Returning zero.",cp);
416 stat = pvr2_ioread_avail(cp);
417 if (stat < 0) return stat;
419 cp->stream_running = !0;
421 mutex_lock(&cp->mutex); do {
423 // Suck data out of the buffers and copy to the user
427 if (!pvr2_ioread_get_buffer(cp)) {
434 if (cp->sync_state == 2) {
435 // We're repeating the sync key data into
437 src = cp->sync_key_ptr + cp->sync_buf_offs;
438 bcnt = cp->sync_key_len - cp->sync_buf_offs;
440 // Normal buffer copy
441 src = cp->c_data_ptr + cp->c_data_offs;
442 bcnt = cp->c_data_len - cp->c_data_offs;
447 // Don't run past user's buffer
448 if (bcnt > cnt) bcnt = cnt;
450 if (copy_to_user(buf,src,bcnt)) {
451 // User supplied a bad pointer?
452 // Give up - this *will* cause data
461 if (cp->sync_state == 2) {
462 // Update offset inside sync key that we're
463 // repeating back out.
464 cp->sync_buf_offs += bcnt;
465 if (cp->sync_buf_offs >= cp->sync_key_len) {
466 // Consumed entire key; switch mode
468 pvr2_trace(PVR2_TRACE_DATA_FLOW,
469 "/*---TRACE_READ---*/"
470 " sync_state <== 0");
474 // Update buffer offset.
475 cp->c_data_offs += bcnt;
479 } while (0); mutex_unlock(&cp->mutex);
483 // If anything was copied, return that count
486 // Nothing copied; suggest to caller that another
487 // attempt should be tried again later
492 pvr2_trace(PVR2_TRACE_DATA_FLOW,
493 "/*---TRACE_READ---*/ pvr2_ioread_read"
494 " id=%p request=%d result=%d",
501 Stuff for Emacs to see, in order to encourage consistent editing style:
502 *** Local Variables: ***
504 *** fill-column: 75 ***
506 *** c-basic-offset: 8 ***