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.
17 #include "pvrusb2-ioread.h"
18 #include "pvrusb2-debug.h"
19 #include <linux/errno.h>
20 #include <linux/string.h>
22 #include <linux/slab.h>
23 #include <linux/mutex.h>
24 #include <linux/uaccess.h>
26 #define BUFFER_COUNT 32
27 #define BUFFER_SIZE PAGE_ALIGN(0x4000)
30 struct pvr2_stream
*stream
;
31 char *buffer_storage
[BUFFER_COUNT
];
33 unsigned int sync_key_len
;
34 unsigned int sync_buf_offs
;
35 unsigned int sync_state
;
36 unsigned int sync_trashed_count
;
37 int enabled
; // Streaming is on
38 int spigot_open
; // OK to pass data to client
39 int stream_running
; // Passing data to client now
41 /* State relevant to current buffer being read */
42 struct pvr2_buffer
*c_buf
;
44 unsigned int c_data_len
;
45 unsigned int c_data_offs
;
49 static int pvr2_ioread_init(struct pvr2_ioread
*cp
)
54 mutex_init(&cp
->mutex
);
56 for (idx
= 0; idx
< BUFFER_COUNT
; idx
++) {
57 cp
->buffer_storage
[idx
] = kmalloc(BUFFER_SIZE
,GFP_KERNEL
);
58 if (!(cp
->buffer_storage
[idx
])) break;
61 if (idx
< BUFFER_COUNT
) {
62 // An allocation appears to have failed
63 for (idx
= 0; idx
< BUFFER_COUNT
; idx
++) {
64 if (!(cp
->buffer_storage
[idx
])) continue;
65 kfree(cp
->buffer_storage
[idx
]);
72 static void pvr2_ioread_done(struct pvr2_ioread
*cp
)
76 pvr2_ioread_setup(cp
,NULL
);
77 for (idx
= 0; idx
< BUFFER_COUNT
; idx
++) {
78 if (!(cp
->buffer_storage
[idx
])) continue;
79 kfree(cp
->buffer_storage
[idx
]);
83 struct pvr2_ioread
*pvr2_ioread_create(void)
85 struct pvr2_ioread
*cp
;
86 cp
= kzalloc(sizeof(*cp
),GFP_KERNEL
);
88 pvr2_trace(PVR2_TRACE_STRUCT
,"pvr2_ioread_create id=%p",cp
);
89 if (pvr2_ioread_init(cp
) < 0) {
96 void pvr2_ioread_destroy(struct pvr2_ioread
*cp
)
100 pvr2_trace(PVR2_TRACE_STRUCT
,"pvr2_ioread_destroy id=%p",cp
);
101 if (cp
->sync_key_ptr
) {
102 kfree(cp
->sync_key_ptr
);
103 cp
->sync_key_ptr
= NULL
;
108 void pvr2_ioread_set_sync_key(struct pvr2_ioread
*cp
,
109 const char *sync_key_ptr
,
110 unsigned int sync_key_len
)
114 if (!sync_key_ptr
) sync_key_len
= 0;
115 if ((sync_key_len
== cp
->sync_key_len
) &&
117 (!memcmp(sync_key_ptr
,cp
->sync_key_ptr
,sync_key_len
)))) return;
119 if (sync_key_len
!= cp
->sync_key_len
) {
120 if (cp
->sync_key_ptr
) {
121 kfree(cp
->sync_key_ptr
);
122 cp
->sync_key_ptr
= NULL
;
124 cp
->sync_key_len
= 0;
126 cp
->sync_key_ptr
= kmalloc(sync_key_len
,GFP_KERNEL
);
127 if (cp
->sync_key_ptr
) {
128 cp
->sync_key_len
= sync_key_len
;
132 if (!cp
->sync_key_len
) return;
133 memcpy(cp
->sync_key_ptr
,sync_key_ptr
,cp
->sync_key_len
);
136 static void pvr2_ioread_stop(struct pvr2_ioread
*cp
)
138 if (!(cp
->enabled
)) return;
139 pvr2_trace(PVR2_TRACE_START_STOP
,
140 "/*---TRACE_READ---*/ pvr2_ioread_stop id=%p",cp
);
141 pvr2_stream_kill(cp
->stream
);
143 cp
->c_data_ptr
= NULL
;
147 cp
->stream_running
= 0;
149 if (cp
->sync_state
) {
150 pvr2_trace(PVR2_TRACE_DATA_FLOW
,
151 "/*---TRACE_READ---*/ sync_state <== 0");
156 static int pvr2_ioread_start(struct pvr2_ioread
*cp
)
159 struct pvr2_buffer
*bp
;
160 if (cp
->enabled
) return 0;
161 if (!(cp
->stream
)) return 0;
162 pvr2_trace(PVR2_TRACE_START_STOP
,
163 "/*---TRACE_READ---*/ pvr2_ioread_start id=%p",cp
);
164 while ((bp
= pvr2_stream_get_idle_buffer(cp
->stream
)) != NULL
) {
165 stat
= pvr2_buffer_queue(bp
);
167 pvr2_trace(PVR2_TRACE_DATA_FLOW
,
168 "/*---TRACE_READ---*/ pvr2_ioread_start id=%p error=%d",
170 pvr2_ioread_stop(cp
);
176 cp
->c_data_ptr
= NULL
;
179 cp
->stream_running
= 0;
180 if (cp
->sync_key_len
) {
181 pvr2_trace(PVR2_TRACE_DATA_FLOW
,
182 "/*---TRACE_READ---*/ sync_state <== 1");
184 cp
->sync_trashed_count
= 0;
185 cp
->sync_buf_offs
= 0;
191 struct pvr2_stream
*pvr2_ioread_get_stream(struct pvr2_ioread
*cp
)
196 int pvr2_ioread_setup(struct pvr2_ioread
*cp
,struct pvr2_stream
*sp
)
200 struct pvr2_buffer
*bp
;
202 mutex_lock(&cp
->mutex
);
205 pvr2_trace(PVR2_TRACE_START_STOP
,
206 "/*---TRACE_READ---*/ pvr2_ioread_setup (tear-down) id=%p",
208 pvr2_ioread_stop(cp
);
209 pvr2_stream_kill(cp
->stream
);
210 if (pvr2_stream_get_buffer_count(cp
->stream
)) {
211 pvr2_stream_set_buffer_count(cp
->stream
,0);
216 pvr2_trace(PVR2_TRACE_START_STOP
,
217 "/*---TRACE_READ---*/ pvr2_ioread_setup (setup) id=%p",
219 pvr2_stream_kill(sp
);
220 ret
= pvr2_stream_set_buffer_count(sp
,BUFFER_COUNT
);
222 mutex_unlock(&cp
->mutex
);
225 for (idx
= 0; idx
< BUFFER_COUNT
; idx
++) {
226 bp
= pvr2_stream_get_buffer(sp
,idx
);
227 pvr2_buffer_set_buffer(bp
,
228 cp
->buffer_storage
[idx
],
234 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
);
247 ret
= pvr2_ioread_start(cp
);
249 pvr2_ioread_stop(cp
);
252 mutex_unlock(&cp
->mutex
);
256 static int pvr2_ioread_get_buffer(struct pvr2_ioread
*cp
)
260 while (cp
->c_data_len
<= cp
->c_data_offs
) {
262 // Flush out current buffer first.
263 stat
= pvr2_buffer_queue(cp
->c_buf
);
265 // Streaming error...
266 pvr2_trace(PVR2_TRACE_DATA_FLOW
,
267 "/*---TRACE_READ---*/ pvr2_ioread_read id=%p queue_error=%d",
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---*/ pvr2_ioread_read id=%p buffer_error=%d",
289 pvr2_ioread_stop(cp
);
297 cp
->c_data_ptr
= cp
->buffer_storage
[
298 pvr2_buffer_get_id(cp
->c_buf
)];
303 static void pvr2_ioread_filter(struct pvr2_ioread
*cp
)
306 if (!cp
->enabled
) return;
307 if (cp
->sync_state
!= 1) return;
309 // Search the stream for our synchronization key. This is made
310 // complicated by the fact that in order to be honest with
311 // ourselves here we must search across buffer boundaries...
312 mutex_lock(&cp
->mutex
);
314 // Ensure we have a buffer
315 if (!pvr2_ioread_get_buffer(cp
)) break;
316 if (!cp
->c_data_len
) break;
318 // Now walk the buffer contents until we match the key or
319 // run out of buffer data.
320 for (idx
= cp
->c_data_offs
; idx
< cp
->c_data_len
; idx
++) {
321 if (cp
->sync_buf_offs
>= cp
->sync_key_len
) break;
322 if (cp
->c_data_ptr
[idx
] ==
323 cp
->sync_key_ptr
[cp
->sync_buf_offs
]) {
324 // Found the next key byte
325 (cp
->sync_buf_offs
)++;
327 // Whoops, mismatched. Start key over...
328 cp
->sync_buf_offs
= 0;
332 // Consume what we've walked through
333 cp
->c_data_offs
+= idx
;
334 cp
->sync_trashed_count
+= idx
;
336 // If we've found the key, then update state and get out.
337 if (cp
->sync_buf_offs
>= cp
->sync_key_len
) {
338 cp
->sync_trashed_count
-= cp
->sync_key_len
;
339 pvr2_trace(PVR2_TRACE_DATA_FLOW
,
340 "/*---TRACE_READ---*/ sync_state <== 2 (skipped %u bytes)",
341 cp
->sync_trashed_count
);
343 cp
->sync_buf_offs
= 0;
347 if (cp
->c_data_offs
< cp
->c_data_len
) {
348 // Sanity check - should NEVER get here
349 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
350 "ERROR: pvr2_ioread filter sync problem len=%u offs=%u",
351 cp
->c_data_len
,cp
->c_data_offs
);
352 // Get out so we don't get stuck in an infinite
357 continue; // (for clarity)
359 mutex_unlock(&cp
->mutex
);
362 int pvr2_ioread_avail(struct pvr2_ioread
*cp
)
365 if (!(cp
->enabled
)) {
366 // Stream is not enabled; so this is an I/O error
370 if (cp
->sync_state
== 1) {
371 pvr2_ioread_filter(cp
);
372 if (cp
->sync_state
== 1) return -EAGAIN
;
376 if (cp
->stream_running
) {
377 if (!pvr2_stream_get_ready_count(cp
->stream
)) {
378 // No data available at all right now.
382 if (pvr2_stream_get_ready_count(cp
->stream
) < BUFFER_COUNT
/2) {
383 // Haven't buffered up enough yet; try again later
388 if ((!(cp
->spigot_open
)) != (!(ret
== 0))) {
389 cp
->spigot_open
= (ret
== 0);
390 pvr2_trace(PVR2_TRACE_DATA_FLOW
,
391 "/*---TRACE_READ---*/ data is %s",
392 cp
->spigot_open
? "available" : "pending");
398 int pvr2_ioread_read(struct pvr2_ioread
*cp
,void __user
*buf
,unsigned int cnt
)
400 unsigned int copied_cnt
;
405 unsigned int req_cnt
= cnt
;
408 pvr2_trace(PVR2_TRACE_TRAP
,
409 "/*---TRACE_READ---*/ pvr2_ioread_read id=%p ZERO Request? Returning zero.",
414 stat
= pvr2_ioread_avail(cp
);
415 if (stat
< 0) return stat
;
417 cp
->stream_running
= !0;
419 mutex_lock(&cp
->mutex
);
422 // Suck data out of the buffers and copy to the user
426 if (!pvr2_ioread_get_buffer(cp
)) {
433 if (cp
->sync_state
== 2) {
434 // We're repeating the sync key data into
436 src
= cp
->sync_key_ptr
+ cp
->sync_buf_offs
;
437 bcnt
= cp
->sync_key_len
- cp
->sync_buf_offs
;
439 // Normal buffer copy
440 src
= cp
->c_data_ptr
+ cp
->c_data_offs
;
441 bcnt
= cp
->c_data_len
- cp
->c_data_offs
;
446 // Don't run past user's buffer
447 if (bcnt
> cnt
) bcnt
= cnt
;
449 if (copy_to_user(buf
,src
,bcnt
)) {
450 // User supplied a bad pointer?
451 // Give up - this *will* cause data
460 if (cp
->sync_state
== 2) {
461 // Update offset inside sync key that we're
462 // repeating back out.
463 cp
->sync_buf_offs
+= bcnt
;
464 if (cp
->sync_buf_offs
>= cp
->sync_key_len
) {
465 // Consumed entire key; switch mode
467 pvr2_trace(PVR2_TRACE_DATA_FLOW
,
468 "/*---TRACE_READ---*/ sync_state <== 0");
472 // Update buffer offset.
473 cp
->c_data_offs
+= bcnt
;
478 mutex_unlock(&cp
->mutex
);
482 // If anything was copied, return that count
485 // Nothing copied; suggest to caller that another
486 // attempt should be tried again later
491 pvr2_trace(PVR2_TRACE_DATA_FLOW
,
492 "/*---TRACE_READ---*/ pvr2_ioread_read id=%p request=%d result=%d",