1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
7 #include "pvrusb2-ioread.h"
8 #include "pvrusb2-debug.h"
9 #include <linux/errno.h>
10 #include <linux/string.h>
12 #include <linux/slab.h>
13 #include <linux/mutex.h>
14 #include <linux/uaccess.h>
16 #define BUFFER_COUNT 32
17 #define BUFFER_SIZE PAGE_ALIGN(0x4000)
20 struct pvr2_stream *stream;
21 char *buffer_storage[BUFFER_COUNT];
23 unsigned int sync_key_len;
24 unsigned int sync_buf_offs;
25 unsigned int sync_state;
26 unsigned int sync_trashed_count;
27 int enabled; // Streaming is on
28 int spigot_open; // OK to pass data to client
29 int stream_running; // Passing data to client now
31 /* State relevant to current buffer being read */
32 struct pvr2_buffer *c_buf;
34 unsigned int c_data_len;
35 unsigned int c_data_offs;
39 static int pvr2_ioread_init(struct pvr2_ioread *cp)
44 mutex_init(&cp->mutex);
46 for (idx = 0; idx < BUFFER_COUNT; idx++) {
47 cp->buffer_storage[idx] = kmalloc(BUFFER_SIZE,GFP_KERNEL);
48 if (!(cp->buffer_storage[idx])) break;
51 if (idx < BUFFER_COUNT) {
52 // An allocation appears to have failed
53 for (idx = 0; idx < BUFFER_COUNT; idx++) {
54 if (!(cp->buffer_storage[idx])) continue;
55 kfree(cp->buffer_storage[idx]);
62 static void pvr2_ioread_done(struct pvr2_ioread *cp)
66 pvr2_ioread_setup(cp,NULL);
67 for (idx = 0; idx < BUFFER_COUNT; idx++) {
68 if (!(cp->buffer_storage[idx])) continue;
69 kfree(cp->buffer_storage[idx]);
73 struct pvr2_ioread *pvr2_ioread_create(void)
75 struct pvr2_ioread *cp;
76 cp = kzalloc(sizeof(*cp),GFP_KERNEL);
78 pvr2_trace(PVR2_TRACE_STRUCT,"pvr2_ioread_create id=%p",cp);
79 if (pvr2_ioread_init(cp) < 0) {
86 void pvr2_ioread_destroy(struct pvr2_ioread *cp)
90 pvr2_trace(PVR2_TRACE_STRUCT,"pvr2_ioread_destroy id=%p",cp);
91 if (cp->sync_key_ptr) {
92 kfree(cp->sync_key_ptr);
93 cp->sync_key_ptr = NULL;
98 void pvr2_ioread_set_sync_key(struct pvr2_ioread *cp,
99 const char *sync_key_ptr,
100 unsigned int sync_key_len)
104 if (!sync_key_ptr) sync_key_len = 0;
105 if ((sync_key_len == cp->sync_key_len) &&
107 (!memcmp(sync_key_ptr,cp->sync_key_ptr,sync_key_len)))) return;
109 if (sync_key_len != cp->sync_key_len) {
110 if (cp->sync_key_ptr) {
111 kfree(cp->sync_key_ptr);
112 cp->sync_key_ptr = NULL;
114 cp->sync_key_len = 0;
116 cp->sync_key_ptr = kmalloc(sync_key_len,GFP_KERNEL);
117 if (cp->sync_key_ptr) {
118 cp->sync_key_len = sync_key_len;
122 if (!cp->sync_key_len) return;
123 memcpy(cp->sync_key_ptr,sync_key_ptr,cp->sync_key_len);
126 static void pvr2_ioread_stop(struct pvr2_ioread *cp)
128 if (!(cp->enabled)) return;
129 pvr2_trace(PVR2_TRACE_START_STOP,
130 "/*---TRACE_READ---*/ pvr2_ioread_stop id=%p",cp);
131 pvr2_stream_kill(cp->stream);
133 cp->c_data_ptr = NULL;
137 cp->stream_running = 0;
139 if (cp->sync_state) {
140 pvr2_trace(PVR2_TRACE_DATA_FLOW,
141 "/*---TRACE_READ---*/ sync_state <== 0");
146 static int pvr2_ioread_start(struct pvr2_ioread *cp)
149 struct pvr2_buffer *bp;
150 if (cp->enabled) return 0;
151 if (!(cp->stream)) return 0;
152 pvr2_trace(PVR2_TRACE_START_STOP,
153 "/*---TRACE_READ---*/ pvr2_ioread_start id=%p",cp);
154 while ((bp = pvr2_stream_get_idle_buffer(cp->stream)) != NULL) {
155 stat = pvr2_buffer_queue(bp);
157 pvr2_trace(PVR2_TRACE_DATA_FLOW,
158 "/*---TRACE_READ---*/ pvr2_ioread_start id=%p error=%d",
160 pvr2_ioread_stop(cp);
166 cp->c_data_ptr = NULL;
169 cp->stream_running = 0;
170 if (cp->sync_key_len) {
171 pvr2_trace(PVR2_TRACE_DATA_FLOW,
172 "/*---TRACE_READ---*/ sync_state <== 1");
174 cp->sync_trashed_count = 0;
175 cp->sync_buf_offs = 0;
181 struct pvr2_stream *pvr2_ioread_get_stream(struct pvr2_ioread *cp)
186 int pvr2_ioread_setup(struct pvr2_ioread *cp,struct pvr2_stream *sp)
190 struct pvr2_buffer *bp;
192 mutex_lock(&cp->mutex);
195 pvr2_trace(PVR2_TRACE_START_STOP,
196 "/*---TRACE_READ---*/ pvr2_ioread_setup (tear-down) id=%p",
198 pvr2_ioread_stop(cp);
199 pvr2_stream_kill(cp->stream);
200 if (pvr2_stream_get_buffer_count(cp->stream)) {
201 pvr2_stream_set_buffer_count(cp->stream,0);
206 pvr2_trace(PVR2_TRACE_START_STOP,
207 "/*---TRACE_READ---*/ pvr2_ioread_setup (setup) id=%p",
209 pvr2_stream_kill(sp);
210 ret = pvr2_stream_set_buffer_count(sp,BUFFER_COUNT);
212 mutex_unlock(&cp->mutex);
215 for (idx = 0; idx < BUFFER_COUNT; idx++) {
216 bp = pvr2_stream_get_buffer(sp,idx);
217 pvr2_buffer_set_buffer(bp,
218 cp->buffer_storage[idx],
224 mutex_unlock(&cp->mutex);
229 int pvr2_ioread_set_enabled(struct pvr2_ioread *cp,int fl)
232 if ((!fl) == (!(cp->enabled))) return ret;
234 mutex_lock(&cp->mutex);
237 ret = pvr2_ioread_start(cp);
239 pvr2_ioread_stop(cp);
242 mutex_unlock(&cp->mutex);
246 static int pvr2_ioread_get_buffer(struct pvr2_ioread *cp)
250 while (cp->c_data_len <= cp->c_data_offs) {
252 // Flush out current buffer first.
253 stat = pvr2_buffer_queue(cp->c_buf);
255 // Streaming error...
256 pvr2_trace(PVR2_TRACE_DATA_FLOW,
257 "/*---TRACE_READ---*/ pvr2_ioread_read id=%p queue_error=%d",
259 pvr2_ioread_stop(cp);
263 cp->c_data_ptr = NULL;
267 // Now get a freshly filled buffer.
268 cp->c_buf = pvr2_stream_get_ready_buffer(cp->stream);
269 if (!cp->c_buf) break; // Nothing ready; done.
270 cp->c_data_len = pvr2_buffer_get_count(cp->c_buf);
271 if (!cp->c_data_len) {
272 // Nothing transferred. Was there an error?
273 stat = pvr2_buffer_get_status(cp->c_buf);
275 // Streaming error...
276 pvr2_trace(PVR2_TRACE_DATA_FLOW,
277 "/*---TRACE_READ---*/ pvr2_ioread_read id=%p buffer_error=%d",
279 pvr2_ioread_stop(cp);
287 cp->c_data_ptr = cp->buffer_storage[
288 pvr2_buffer_get_id(cp->c_buf)];
293 static void pvr2_ioread_filter(struct pvr2_ioread *cp)
296 if (!cp->enabled) return;
297 if (cp->sync_state != 1) return;
299 // Search the stream for our synchronization key. This is made
300 // complicated by the fact that in order to be honest with
301 // ourselves here we must search across buffer boundaries...
302 mutex_lock(&cp->mutex);
304 // Ensure we have a buffer
305 if (!pvr2_ioread_get_buffer(cp)) break;
306 if (!cp->c_data_len) break;
308 // Now walk the buffer contents until we match the key or
309 // run out of buffer data.
310 for (idx = cp->c_data_offs; idx < cp->c_data_len; idx++) {
311 if (cp->sync_buf_offs >= cp->sync_key_len) break;
312 if (cp->c_data_ptr[idx] ==
313 cp->sync_key_ptr[cp->sync_buf_offs]) {
314 // Found the next key byte
315 (cp->sync_buf_offs)++;
317 // Whoops, mismatched. Start key over...
318 cp->sync_buf_offs = 0;
322 // Consume what we've walked through
323 cp->c_data_offs += idx;
324 cp->sync_trashed_count += idx;
326 // If we've found the key, then update state and get out.
327 if (cp->sync_buf_offs >= cp->sync_key_len) {
328 cp->sync_trashed_count -= cp->sync_key_len;
329 pvr2_trace(PVR2_TRACE_DATA_FLOW,
330 "/*---TRACE_READ---*/ sync_state <== 2 (skipped %u bytes)",
331 cp->sync_trashed_count);
333 cp->sync_buf_offs = 0;
337 if (cp->c_data_offs < cp->c_data_len) {
338 // Sanity check - should NEVER get here
339 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
340 "ERROR: pvr2_ioread filter sync problem len=%u offs=%u",
341 cp->c_data_len,cp->c_data_offs);
342 // Get out so we don't get stuck in an infinite
347 continue; // (for clarity)
349 mutex_unlock(&cp->mutex);
352 int pvr2_ioread_avail(struct pvr2_ioread *cp)
355 if (!(cp->enabled)) {
356 // Stream is not enabled; so this is an I/O error
360 if (cp->sync_state == 1) {
361 pvr2_ioread_filter(cp);
362 if (cp->sync_state == 1) return -EAGAIN;
366 if (cp->stream_running) {
367 if (!pvr2_stream_get_ready_count(cp->stream)) {
368 // No data available at all right now.
372 if (pvr2_stream_get_ready_count(cp->stream) < BUFFER_COUNT/2) {
373 // Haven't buffered up enough yet; try again later
378 if ((!(cp->spigot_open)) != (!(ret == 0))) {
379 cp->spigot_open = (ret == 0);
380 pvr2_trace(PVR2_TRACE_DATA_FLOW,
381 "/*---TRACE_READ---*/ data is %s",
382 cp->spigot_open ? "available" : "pending");
388 int pvr2_ioread_read(struct pvr2_ioread *cp,void __user *buf,unsigned int cnt)
390 unsigned int copied_cnt;
395 unsigned int req_cnt = cnt;
398 pvr2_trace(PVR2_TRACE_TRAP,
399 "/*---TRACE_READ---*/ pvr2_ioread_read id=%p ZERO Request? Returning zero.",
404 stat = pvr2_ioread_avail(cp);
405 if (stat < 0) return stat;
407 cp->stream_running = !0;
409 mutex_lock(&cp->mutex);
412 // Suck data out of the buffers and copy to the user
416 if (!pvr2_ioread_get_buffer(cp)) {
423 if (cp->sync_state == 2) {
424 // We're repeating the sync key data into
426 src = cp->sync_key_ptr + cp->sync_buf_offs;
427 bcnt = cp->sync_key_len - cp->sync_buf_offs;
429 // Normal buffer copy
430 src = cp->c_data_ptr + cp->c_data_offs;
431 bcnt = cp->c_data_len - cp->c_data_offs;
436 // Don't run past user's buffer
437 if (bcnt > cnt) bcnt = cnt;
439 if (copy_to_user(buf,src,bcnt)) {
440 // User supplied a bad pointer?
441 // Give up - this *will* cause data
450 if (cp->sync_state == 2) {
451 // Update offset inside sync key that we're
452 // repeating back out.
453 cp->sync_buf_offs += bcnt;
454 if (cp->sync_buf_offs >= cp->sync_key_len) {
455 // Consumed entire key; switch mode
457 pvr2_trace(PVR2_TRACE_DATA_FLOW,
458 "/*---TRACE_READ---*/ sync_state <== 0");
462 // Update buffer offset.
463 cp->c_data_offs += bcnt;
468 mutex_unlock(&cp->mutex);
472 // If anything was copied, return that count
475 // Nothing copied; suggest to caller that another
476 // attempt should be tried again later
481 pvr2_trace(PVR2_TRACE_DATA_FLOW,
482 "/*---TRACE_READ---*/ pvr2_ioread_read id=%p request=%d result=%d",