1 /*
2  *
3  *
4  *  Copyright (C) 2005 Mike Isely <isely@pobox.com>
5  *
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
9  *
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.
14  *
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
18  *
19  */
20 
21 #include "pvrusb2-ioread.h"
22 #include "pvrusb2-debug.h"
23 #include <linux/errno.h>
24 #include <linux/string.h>
25 #include <linux/mm.h>
26 #include <linux/slab.h>
27 #include <linux/mutex.h>
28 #include <linux/uaccess.h>
29 
30 #define BUFFER_COUNT 32
31 #define BUFFER_SIZE PAGE_ALIGN(0x4000)
32 
33 struct pvr2_ioread {
34 	struct pvr2_stream *stream;
35 	char *buffer_storage[BUFFER_COUNT];
36 	char *sync_key_ptr;
37 	unsigned int sync_key_len;
38 	unsigned int sync_buf_offs;
39 	unsigned int sync_state;
40 	unsigned int sync_trashed_count;
41 	int enabled;         // Streaming is on
42 	int spigot_open;     // OK to pass data to client
43 	int stream_running;  // Passing data to client now
44 
45 	/* State relevant to current buffer being read */
46 	struct pvr2_buffer *c_buf;
47 	char *c_data_ptr;
48 	unsigned int c_data_len;
49 	unsigned int c_data_offs;
50 	struct mutex mutex;
51 };
52 
53 static int pvr2_ioread_init(struct pvr2_ioread *cp)
54 {
55 	unsigned int idx;
56 
57 	cp->stream = NULL;
58 	mutex_init(&cp->mutex);
59 
60 	for (idx = 0; idx < BUFFER_COUNT; idx++) {
61 		cp->buffer_storage[idx] = kmalloc(BUFFER_SIZE,GFP_KERNEL);
62 		if (!(cp->buffer_storage[idx])) break;
63 	}
64 
65 	if (idx < BUFFER_COUNT) {
66 		// An allocation appears to have failed
67 		for (idx = 0; idx < BUFFER_COUNT; idx++) {
68 			if (!(cp->buffer_storage[idx])) continue;
69 			kfree(cp->buffer_storage[idx]);
70 		}
71 		return -ENOMEM;
72 	}
73 	return 0;
74 }
75 
76 static void pvr2_ioread_done(struct pvr2_ioread *cp)
77 {
78 	unsigned int idx;
79 
80 	pvr2_ioread_setup(cp,NULL);
81 	for (idx = 0; idx < BUFFER_COUNT; idx++) {
82 		if (!(cp->buffer_storage[idx])) continue;
83 		kfree(cp->buffer_storage[idx]);
84 	}
85 }
86 
87 struct pvr2_ioread *pvr2_ioread_create(void)
88 {
89 	struct pvr2_ioread *cp;
90 	cp = kzalloc(sizeof(*cp),GFP_KERNEL);
91 	if (!cp) return NULL;
92 	pvr2_trace(PVR2_TRACE_STRUCT,"pvr2_ioread_create id=%p",cp);
93 	if (pvr2_ioread_init(cp) < 0) {
94 		kfree(cp);
95 		return NULL;
96 	}
97 	return cp;
98 }
99 
100 void pvr2_ioread_destroy(struct pvr2_ioread *cp)
101 {
102 	if (!cp) return;
103 	pvr2_ioread_done(cp);
104 	pvr2_trace(PVR2_TRACE_STRUCT,"pvr2_ioread_destroy id=%p",cp);
105 	if (cp->sync_key_ptr) {
106 		kfree(cp->sync_key_ptr);
107 		cp->sync_key_ptr = NULL;
108 	}
109 	kfree(cp);
110 }
111 
112 void pvr2_ioread_set_sync_key(struct pvr2_ioread *cp,
113 			      const char *sync_key_ptr,
114 			      unsigned int sync_key_len)
115 {
116 	if (!cp) return;
117 
118 	if (!sync_key_ptr) sync_key_len = 0;
119 	if ((sync_key_len == cp->sync_key_len) &&
120 	    ((!sync_key_len) ||
121 	     (!memcmp(sync_key_ptr,cp->sync_key_ptr,sync_key_len)))) return;
122 
123 	if (sync_key_len != cp->sync_key_len) {
124 		if (cp->sync_key_ptr) {
125 			kfree(cp->sync_key_ptr);
126 			cp->sync_key_ptr = NULL;
127 		}
128 		cp->sync_key_len = 0;
129 		if (sync_key_len) {
130 			cp->sync_key_ptr = kmalloc(sync_key_len,GFP_KERNEL);
131 			if (cp->sync_key_ptr) {
132 				cp->sync_key_len = sync_key_len;
133 			}
134 		}
135 	}
136 	if (!cp->sync_key_len) return;
137 	memcpy(cp->sync_key_ptr,sync_key_ptr,cp->sync_key_len);
138 }
139 
140 static void pvr2_ioread_stop(struct pvr2_ioread *cp)
141 {
142 	if (!(cp->enabled)) return;
143 	pvr2_trace(PVR2_TRACE_START_STOP,
144 		   "/*---TRACE_READ---*/ pvr2_ioread_stop id=%p",cp);
145 	pvr2_stream_kill(cp->stream);
146 	cp->c_buf = NULL;
147 	cp->c_data_ptr = NULL;
148 	cp->c_data_len = 0;
149 	cp->c_data_offs = 0;
150 	cp->enabled = 0;
151 	cp->stream_running = 0;
152 	cp->spigot_open = 0;
153 	if (cp->sync_state) {
154 		pvr2_trace(PVR2_TRACE_DATA_FLOW,
155 			   "/*---TRACE_READ---*/ sync_state <== 0");
156 		cp->sync_state = 0;
157 	}
158 }
159 
160 static int pvr2_ioread_start(struct pvr2_ioread *cp)
161 {
162 	int stat;
163 	struct pvr2_buffer *bp;
164 	if (cp->enabled) return 0;
165 	if (!(cp->stream)) return 0;
166 	pvr2_trace(PVR2_TRACE_START_STOP,
167 		   "/*---TRACE_READ---*/ pvr2_ioread_start id=%p",cp);
168 	while ((bp = pvr2_stream_get_idle_buffer(cp->stream)) != NULL) {
169 		stat = pvr2_buffer_queue(bp);
170 		if (stat < 0) {
171 			pvr2_trace(PVR2_TRACE_DATA_FLOW,
172 				   "/*---TRACE_READ---*/ pvr2_ioread_start id=%p error=%d",
173 				   cp,stat);
174 			pvr2_ioread_stop(cp);
175 			return stat;
176 		}
177 	}
178 	cp->enabled = !0;
179 	cp->c_buf = NULL;
180 	cp->c_data_ptr = NULL;
181 	cp->c_data_len = 0;
182 	cp->c_data_offs = 0;
183 	cp->stream_running = 0;
184 	if (cp->sync_key_len) {
185 		pvr2_trace(PVR2_TRACE_DATA_FLOW,
186 			   "/*---TRACE_READ---*/ sync_state <== 1");
187 		cp->sync_state = 1;
188 		cp->sync_trashed_count = 0;
189 		cp->sync_buf_offs = 0;
190 	}
191 	cp->spigot_open = 0;
192 	return 0;
193 }
194 
195 struct pvr2_stream *pvr2_ioread_get_stream(struct pvr2_ioread *cp)
196 {
197 	return cp->stream;
198 }
199 
200 int pvr2_ioread_setup(struct pvr2_ioread *cp,struct pvr2_stream *sp)
201 {
202 	int ret;
203 	unsigned int idx;
204 	struct pvr2_buffer *bp;
205 
206 	mutex_lock(&cp->mutex);
207 	do {
208 		if (cp->stream) {
209 			pvr2_trace(PVR2_TRACE_START_STOP,
210 				   "/*---TRACE_READ---*/ pvr2_ioread_setup (tear-down) id=%p",
211 				   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);
216 			}
217 			cp->stream = NULL;
218 		}
219 		if (sp) {
220 			pvr2_trace(PVR2_TRACE_START_STOP,
221 				   "/*---TRACE_READ---*/ pvr2_ioread_setup (setup) id=%p",
222 				   cp);
223 			pvr2_stream_kill(sp);
224 			ret = pvr2_stream_set_buffer_count(sp,BUFFER_COUNT);
225 			if (ret < 0) {
226 				mutex_unlock(&cp->mutex);
227 				return ret;
228 			}
229 			for (idx = 0; idx < BUFFER_COUNT; idx++) {
230 				bp = pvr2_stream_get_buffer(sp,idx);
231 				pvr2_buffer_set_buffer(bp,
232 						       cp->buffer_storage[idx],
233 						       BUFFER_SIZE);
234 			}
235 			cp->stream = sp;
236 		}
237 	} while (0);
238 	mutex_unlock(&cp->mutex);
239 
240 	return 0;
241 }
242 
243 int pvr2_ioread_set_enabled(struct pvr2_ioread *cp,int fl)
244 {
245 	int ret = 0;
246 	if ((!fl) == (!(cp->enabled))) return ret;
247 
248 	mutex_lock(&cp->mutex);
249 	do {
250 		if (fl) {
251 			ret = pvr2_ioread_start(cp);
252 		} else {
253 			pvr2_ioread_stop(cp);
254 		}
255 	} while (0);
256 	mutex_unlock(&cp->mutex);
257 	return ret;
258 }
259 
260 static int pvr2_ioread_get_buffer(struct pvr2_ioread *cp)
261 {
262 	int stat;
263 
264 	while (cp->c_data_len <= cp->c_data_offs) {
265 		if (cp->c_buf) {
266 			// Flush out current buffer first.
267 			stat = pvr2_buffer_queue(cp->c_buf);
268 			if (stat < 0) {
269 				// Streaming error...
270 				pvr2_trace(PVR2_TRACE_DATA_FLOW,
271 					   "/*---TRACE_READ---*/ pvr2_ioread_read id=%p queue_error=%d",
272 					   cp,stat);
273 				pvr2_ioread_stop(cp);
274 				return 0;
275 			}
276 			cp->c_buf = NULL;
277 			cp->c_data_ptr = NULL;
278 			cp->c_data_len = 0;
279 			cp->c_data_offs = 0;
280 		}
281 		// Now get a freshly filled buffer.
282 		cp->c_buf = pvr2_stream_get_ready_buffer(cp->stream);
283 		if (!cp->c_buf) break; // Nothing ready; done.
284 		cp->c_data_len = pvr2_buffer_get_count(cp->c_buf);
285 		if (!cp->c_data_len) {
286 			// Nothing transferred.  Was there an error?
287 			stat = pvr2_buffer_get_status(cp->c_buf);
288 			if (stat < 0) {
289 				// Streaming error...
290 				pvr2_trace(PVR2_TRACE_DATA_FLOW,
291 					   "/*---TRACE_READ---*/ pvr2_ioread_read id=%p buffer_error=%d",
292 					   cp,stat);
293 				pvr2_ioread_stop(cp);
294 				// Give up.
295 				return 0;
296 			}
297 			// Start over...
298 			continue;
299 		}
300 		cp->c_data_offs = 0;
301 		cp->c_data_ptr = cp->buffer_storage[
302 			pvr2_buffer_get_id(cp->c_buf)];
303 	}
304 	return !0;
305 }
306 
307 static void pvr2_ioread_filter(struct pvr2_ioread *cp)
308 {
309 	unsigned int idx;
310 	if (!cp->enabled) return;
311 	if (cp->sync_state != 1) return;
312 
313 	// Search the stream for our synchronization key.  This is made
314 	// complicated by the fact that in order to be honest with
315 	// ourselves here we must search across buffer boundaries...
316 	mutex_lock(&cp->mutex);
317 	while (1) {
318 		// Ensure we have a buffer
319 		if (!pvr2_ioread_get_buffer(cp)) break;
320 		if (!cp->c_data_len) break;
321 
322 		// Now walk the buffer contents until we match the key or
323 		// run out of buffer data.
324 		for (idx = cp->c_data_offs; idx < cp->c_data_len; idx++) {
325 			if (cp->sync_buf_offs >= cp->sync_key_len) break;
326 			if (cp->c_data_ptr[idx] ==
327 			    cp->sync_key_ptr[cp->sync_buf_offs]) {
328 				// Found the next key byte
329 				(cp->sync_buf_offs)++;
330 			} else {
331 				// Whoops, mismatched.  Start key over...
332 				cp->sync_buf_offs = 0;
333 			}
334 		}
335 
336 		// Consume what we've walked through
337 		cp->c_data_offs += idx;
338 		cp->sync_trashed_count += idx;
339 
340 		// If we've found the key, then update state and get out.
341 		if (cp->sync_buf_offs >= cp->sync_key_len) {
342 			cp->sync_trashed_count -= cp->sync_key_len;
343 			pvr2_trace(PVR2_TRACE_DATA_FLOW,
344 				   "/*---TRACE_READ---*/ sync_state <== 2 (skipped %u bytes)",
345 				   cp->sync_trashed_count);
346 			cp->sync_state = 2;
347 			cp->sync_buf_offs = 0;
348 			break;
349 		}
350 
351 		if (cp->c_data_offs < cp->c_data_len) {
352 			// Sanity check - should NEVER get here
353 			pvr2_trace(PVR2_TRACE_ERROR_LEGS,
354 				   "ERROR: pvr2_ioread filter sync problem len=%u offs=%u",
355 				   cp->c_data_len,cp->c_data_offs);
356 			// Get out so we don't get stuck in an infinite
357 			// loop.
358 			break;
359 		}
360 
361 		continue; // (for clarity)
362 	}
363 	mutex_unlock(&cp->mutex);
364 }
365 
366 int pvr2_ioread_avail(struct pvr2_ioread *cp)
367 {
368 	int ret;
369 	if (!(cp->enabled)) {
370 		// Stream is not enabled; so this is an I/O error
371 		return -EIO;
372 	}
373 
374 	if (cp->sync_state == 1) {
375 		pvr2_ioread_filter(cp);
376 		if (cp->sync_state == 1) return -EAGAIN;
377 	}
378 
379 	ret = 0;
380 	if (cp->stream_running) {
381 		if (!pvr2_stream_get_ready_count(cp->stream)) {
382 			// No data available at all right now.
383 			ret = -EAGAIN;
384 		}
385 	} else {
386 		if (pvr2_stream_get_ready_count(cp->stream) < BUFFER_COUNT/2) {
387 			// Haven't buffered up enough yet; try again later
388 			ret = -EAGAIN;
389 		}
390 	}
391 
392 	if ((!(cp->spigot_open)) != (!(ret == 0))) {
393 		cp->spigot_open = (ret == 0);
394 		pvr2_trace(PVR2_TRACE_DATA_FLOW,
395 			   "/*---TRACE_READ---*/ data is %s",
396 			   cp->spigot_open ? "available" : "pending");
397 	}
398 
399 	return ret;
400 }
401 
402 int pvr2_ioread_read(struct pvr2_ioread *cp,void __user *buf,unsigned int cnt)
403 {
404 	unsigned int copied_cnt;
405 	unsigned int bcnt;
406 	const char *src;
407 	int stat;
408 	int ret = 0;
409 	unsigned int req_cnt = cnt;
410 
411 	if (!cnt) {
412 		pvr2_trace(PVR2_TRACE_TRAP,
413 			   "/*---TRACE_READ---*/ pvr2_ioread_read id=%p ZERO Request? Returning zero.",
414 cp);
415 		return 0;
416 	}
417 
418 	stat = pvr2_ioread_avail(cp);
419 	if (stat < 0) return stat;
420 
421 	cp->stream_running = !0;
422 
423 	mutex_lock(&cp->mutex);
424 	do {
425 
426 		// Suck data out of the buffers and copy to the user
427 		copied_cnt = 0;
428 		if (!buf) cnt = 0;
429 		while (1) {
430 			if (!pvr2_ioread_get_buffer(cp)) {
431 				ret = -EIO;
432 				break;
433 			}
434 
435 			if (!cnt) break;
436 
437 			if (cp->sync_state == 2) {
438 				// We're repeating the sync key data into
439 				// the stream.
440 				src = cp->sync_key_ptr + cp->sync_buf_offs;
441 				bcnt = cp->sync_key_len - cp->sync_buf_offs;
442 			} else {
443 				// Normal buffer copy
444 				src = cp->c_data_ptr + cp->c_data_offs;
445 				bcnt = cp->c_data_len - cp->c_data_offs;
446 			}
447 
448 			if (!bcnt) break;
449 
450 			// Don't run past user's buffer
451 			if (bcnt > cnt) bcnt = cnt;
452 
453 			if (copy_to_user(buf,src,bcnt)) {
454 				// User supplied a bad pointer?
455 				// Give up - this *will* cause data
456 				// to be lost.
457 				ret = -EFAULT;
458 				break;
459 			}
460 			cnt -= bcnt;
461 			buf += bcnt;
462 			copied_cnt += bcnt;
463 
464 			if (cp->sync_state == 2) {
465 				// Update offset inside sync key that we're
466 				// repeating back out.
467 				cp->sync_buf_offs += bcnt;
468 				if (cp->sync_buf_offs >= cp->sync_key_len) {
469 					// Consumed entire key; switch mode
470 					// to normal.
471 					pvr2_trace(PVR2_TRACE_DATA_FLOW,
472 						   "/*---TRACE_READ---*/ sync_state <== 0");
473 					cp->sync_state = 0;
474 				}
475 			} else {
476 				// Update buffer offset.
477 				cp->c_data_offs += bcnt;
478 			}
479 		}
480 
481 	} while (0);
482 	mutex_unlock(&cp->mutex);
483 
484 	if (!ret) {
485 		if (copied_cnt) {
486 			// If anything was copied, return that count
487 			ret = copied_cnt;
488 		} else {
489 			// Nothing copied; suggest to caller that another
490 			// attempt should be tried again later
491 			ret = -EAGAIN;
492 		}
493 	}
494 
495 	pvr2_trace(PVR2_TRACE_DATA_FLOW,
496 		   "/*---TRACE_READ---*/ pvr2_ioread_read id=%p request=%d result=%d",
497 		   cp,req_cnt,ret);
498 	return ret;
499 }
500