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 <asm/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---*/"
173 				   " pvr2_ioread_start id=%p"
174 				   " error=%d",
175 				   cp,stat);
176 			pvr2_ioread_stop(cp);
177 			return stat;
178 		}
179 	}
180 	cp->enabled = !0;
181 	cp->c_buf = NULL;
182 	cp->c_data_ptr = NULL;
183 	cp->c_data_len = 0;
184 	cp->c_data_offs = 0;
185 	cp->stream_running = 0;
186 	if (cp->sync_key_len) {
187 		pvr2_trace(PVR2_TRACE_DATA_FLOW,
188 			   "/*---TRACE_READ---*/ sync_state <== 1");
189 		cp->sync_state = 1;
190 		cp->sync_trashed_count = 0;
191 		cp->sync_buf_offs = 0;
192 	}
193 	cp->spigot_open = 0;
194 	return 0;
195 }
196 
197 struct pvr2_stream *pvr2_ioread_get_stream(struct pvr2_ioread *cp)
198 {
199 	return cp->stream;
200 }
201 
202 int pvr2_ioread_setup(struct pvr2_ioread *cp,struct pvr2_stream *sp)
203 {
204 	int ret;
205 	unsigned int idx;
206 	struct pvr2_buffer *bp;
207 
208 	mutex_lock(&cp->mutex);
209 	do {
210 		if (cp->stream) {
211 			pvr2_trace(PVR2_TRACE_START_STOP,
212 				   "/*---TRACE_READ---*/"
213 				   " pvr2_ioread_setup (tear-down) id=%p",cp);
214 			pvr2_ioread_stop(cp);
215 			pvr2_stream_kill(cp->stream);
216 			if (pvr2_stream_get_buffer_count(cp->stream)) {
217 				pvr2_stream_set_buffer_count(cp->stream,0);
218 			}
219 			cp->stream = NULL;
220 		}
221 		if (sp) {
222 			pvr2_trace(PVR2_TRACE_START_STOP,
223 				   "/*---TRACE_READ---*/"
224 				   " pvr2_ioread_setup (setup) id=%p",cp);
225 			pvr2_stream_kill(sp);
226 			ret = pvr2_stream_set_buffer_count(sp,BUFFER_COUNT);
227 			if (ret < 0) {
228 				mutex_unlock(&cp->mutex);
229 				return ret;
230 			}
231 			for (idx = 0; idx < BUFFER_COUNT; idx++) {
232 				bp = pvr2_stream_get_buffer(sp,idx);
233 				pvr2_buffer_set_buffer(bp,
234 						       cp->buffer_storage[idx],
235 						       BUFFER_SIZE);
236 			}
237 			cp->stream = sp;
238 		}
239 	} while (0);
240 	mutex_unlock(&cp->mutex);
241 
242 	return 0;
243 }
244 
245 int pvr2_ioread_set_enabled(struct pvr2_ioread *cp,int fl)
246 {
247 	int ret = 0;
248 	if ((!fl) == (!(cp->enabled))) return ret;
249 
250 	mutex_lock(&cp->mutex);
251 	do {
252 		if (fl) {
253 			ret = pvr2_ioread_start(cp);
254 		} else {
255 			pvr2_ioread_stop(cp);
256 		}
257 	} while (0);
258 	mutex_unlock(&cp->mutex);
259 	return ret;
260 }
261 
262 static int pvr2_ioread_get_buffer(struct pvr2_ioread *cp)
263 {
264 	int stat;
265 
266 	while (cp->c_data_len <= cp->c_data_offs) {
267 		if (cp->c_buf) {
268 			// Flush out current buffer first.
269 			stat = pvr2_buffer_queue(cp->c_buf);
270 			if (stat < 0) {
271 				// Streaming error...
272 				pvr2_trace(PVR2_TRACE_DATA_FLOW,
273 					   "/*---TRACE_READ---*/"
274 					   " pvr2_ioread_read id=%p"
275 					   " queue_error=%d",
276 					   cp,stat);
277 				pvr2_ioread_stop(cp);
278 				return 0;
279 			}
280 			cp->c_buf = NULL;
281 			cp->c_data_ptr = NULL;
282 			cp->c_data_len = 0;
283 			cp->c_data_offs = 0;
284 		}
285 		// Now get a freshly filled buffer.
286 		cp->c_buf = pvr2_stream_get_ready_buffer(cp->stream);
287 		if (!cp->c_buf) break; // Nothing ready; done.
288 		cp->c_data_len = pvr2_buffer_get_count(cp->c_buf);
289 		if (!cp->c_data_len) {
290 			// Nothing transferred.  Was there an error?
291 			stat = pvr2_buffer_get_status(cp->c_buf);
292 			if (stat < 0) {
293 				// Streaming error...
294 				pvr2_trace(PVR2_TRACE_DATA_FLOW,
295 					   "/*---TRACE_READ---*/"
296 					   " pvr2_ioread_read id=%p"
297 					   " buffer_error=%d",
298 					   cp,stat);
299 				pvr2_ioread_stop(cp);
300 				// Give up.
301 				return 0;
302 			}
303 			// Start over...
304 			continue;
305 		}
306 		cp->c_data_offs = 0;
307 		cp->c_data_ptr = cp->buffer_storage[
308 			pvr2_buffer_get_id(cp->c_buf)];
309 	}
310 	return !0;
311 }
312 
313 static void pvr2_ioread_filter(struct pvr2_ioread *cp)
314 {
315 	unsigned int idx;
316 	if (!cp->enabled) return;
317 	if (cp->sync_state != 1) return;
318 
319 	// Search the stream for our synchronization key.  This is made
320 	// complicated by the fact that in order to be honest with
321 	// ourselves here we must search across buffer boundaries...
322 	mutex_lock(&cp->mutex);
323 	while (1) {
324 		// Ensure we have a buffer
325 		if (!pvr2_ioread_get_buffer(cp)) break;
326 		if (!cp->c_data_len) break;
327 
328 		// Now walk the buffer contents until we match the key or
329 		// run out of buffer data.
330 		for (idx = cp->c_data_offs; idx < cp->c_data_len; idx++) {
331 			if (cp->sync_buf_offs >= cp->sync_key_len) break;
332 			if (cp->c_data_ptr[idx] ==
333 			    cp->sync_key_ptr[cp->sync_buf_offs]) {
334 				// Found the next key byte
335 				(cp->sync_buf_offs)++;
336 			} else {
337 				// Whoops, mismatched.  Start key over...
338 				cp->sync_buf_offs = 0;
339 			}
340 		}
341 
342 		// Consume what we've walked through
343 		cp->c_data_offs += idx;
344 		cp->sync_trashed_count += idx;
345 
346 		// If we've found the key, then update state and get out.
347 		if (cp->sync_buf_offs >= cp->sync_key_len) {
348 			cp->sync_trashed_count -= cp->sync_key_len;
349 			pvr2_trace(PVR2_TRACE_DATA_FLOW,
350 				   "/*---TRACE_READ---*/"
351 				   " sync_state <== 2 (skipped %u bytes)",
352 				   cp->sync_trashed_count);
353 			cp->sync_state = 2;
354 			cp->sync_buf_offs = 0;
355 			break;
356 		}
357 
358 		if (cp->c_data_offs < cp->c_data_len) {
359 			// Sanity check - should NEVER get here
360 			pvr2_trace(PVR2_TRACE_ERROR_LEGS,
361 				   "ERROR: pvr2_ioread filter sync problem"
362 				   " len=%u offs=%u",
363 				   cp->c_data_len,cp->c_data_offs);
364 			// Get out so we don't get stuck in an infinite
365 			// loop.
366 			break;
367 		}
368 
369 		continue; // (for clarity)
370 	}
371 	mutex_unlock(&cp->mutex);
372 }
373 
374 int pvr2_ioread_avail(struct pvr2_ioread *cp)
375 {
376 	int ret;
377 	if (!(cp->enabled)) {
378 		// Stream is not enabled; so this is an I/O error
379 		return -EIO;
380 	}
381 
382 	if (cp->sync_state == 1) {
383 		pvr2_ioread_filter(cp);
384 		if (cp->sync_state == 1) return -EAGAIN;
385 	}
386 
387 	ret = 0;
388 	if (cp->stream_running) {
389 		if (!pvr2_stream_get_ready_count(cp->stream)) {
390 			// No data available at all right now.
391 			ret = -EAGAIN;
392 		}
393 	} else {
394 		if (pvr2_stream_get_ready_count(cp->stream) < BUFFER_COUNT/2) {
395 			// Haven't buffered up enough yet; try again later
396 			ret = -EAGAIN;
397 		}
398 	}
399 
400 	if ((!(cp->spigot_open)) != (!(ret == 0))) {
401 		cp->spigot_open = (ret == 0);
402 		pvr2_trace(PVR2_TRACE_DATA_FLOW,
403 			   "/*---TRACE_READ---*/ data is %s",
404 			   cp->spigot_open ? "available" : "pending");
405 	}
406 
407 	return ret;
408 }
409 
410 int pvr2_ioread_read(struct pvr2_ioread *cp,void __user *buf,unsigned int cnt)
411 {
412 	unsigned int copied_cnt;
413 	unsigned int bcnt;
414 	const char *src;
415 	int stat;
416 	int ret = 0;
417 	unsigned int req_cnt = cnt;
418 
419 	if (!cnt) {
420 		pvr2_trace(PVR2_TRACE_TRAP,
421 			   "/*---TRACE_READ---*/ pvr2_ioread_read id=%p"
422 			   " ZERO Request? Returning zero.",cp);
423 		return 0;
424 	}
425 
426 	stat = pvr2_ioread_avail(cp);
427 	if (stat < 0) return stat;
428 
429 	cp->stream_running = !0;
430 
431 	mutex_lock(&cp->mutex);
432 	do {
433 
434 		// Suck data out of the buffers and copy to the user
435 		copied_cnt = 0;
436 		if (!buf) cnt = 0;
437 		while (1) {
438 			if (!pvr2_ioread_get_buffer(cp)) {
439 				ret = -EIO;
440 				break;
441 			}
442 
443 			if (!cnt) break;
444 
445 			if (cp->sync_state == 2) {
446 				// We're repeating the sync key data into
447 				// the stream.
448 				src = cp->sync_key_ptr + cp->sync_buf_offs;
449 				bcnt = cp->sync_key_len - cp->sync_buf_offs;
450 			} else {
451 				// Normal buffer copy
452 				src = cp->c_data_ptr + cp->c_data_offs;
453 				bcnt = cp->c_data_len - cp->c_data_offs;
454 			}
455 
456 			if (!bcnt) break;
457 
458 			// Don't run past user's buffer
459 			if (bcnt > cnt) bcnt = cnt;
460 
461 			if (copy_to_user(buf,src,bcnt)) {
462 				// User supplied a bad pointer?
463 				// Give up - this *will* cause data
464 				// to be lost.
465 				ret = -EFAULT;
466 				break;
467 			}
468 			cnt -= bcnt;
469 			buf += bcnt;
470 			copied_cnt += bcnt;
471 
472 			if (cp->sync_state == 2) {
473 				// Update offset inside sync key that we're
474 				// repeating back out.
475 				cp->sync_buf_offs += bcnt;
476 				if (cp->sync_buf_offs >= cp->sync_key_len) {
477 					// Consumed entire key; switch mode
478 					// to normal.
479 					pvr2_trace(PVR2_TRACE_DATA_FLOW,
480 						   "/*---TRACE_READ---*/"
481 						   " sync_state <== 0");
482 					cp->sync_state = 0;
483 				}
484 			} else {
485 				// Update buffer offset.
486 				cp->c_data_offs += bcnt;
487 			}
488 		}
489 
490 	} while (0);
491 	mutex_unlock(&cp->mutex);
492 
493 	if (!ret) {
494 		if (copied_cnt) {
495 			// If anything was copied, return that count
496 			ret = copied_cnt;
497 		} else {
498 			// Nothing copied; suggest to caller that another
499 			// attempt should be tried again later
500 			ret = -EAGAIN;
501 		}
502 	}
503 
504 	pvr2_trace(PVR2_TRACE_DATA_FLOW,
505 		   "/*---TRACE_READ---*/ pvr2_ioread_read"
506 		   " id=%p request=%d result=%d",
507 		   cp,req_cnt,ret);
508 	return ret;
509 }
510