]>
Commit | Line | Data |
---|---|---|
d855497e MI |
1 | /* |
2 | * | |
3 | * $Id$ | |
4 | * | |
5 | * Copyright (C) 2005 Mike Isely <isely@pobox.com> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | * | |
20 | */ | |
21 | ||
22 | #include "pvrusb2-ioread.h" | |
23 | #include "pvrusb2-debug.h" | |
24 | #include <linux/errno.h> | |
25 | #include <linux/string.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 | ||
a0fd1cb1 | 57 | cp->stream = NULL; |
d855497e MI |
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 | ||
a0fd1cb1 | 80 | pvr2_ioread_setup(cp,NULL); |
d855497e MI |
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; | |
ca545f7c | 90 | cp = kzalloc(sizeof(*cp),GFP_KERNEL); |
a0fd1cb1 | 91 | if (!cp) return NULL; |
d855497e | 92 | pvr2_trace(PVR2_TRACE_STRUCT,"pvr2_ioread_create id=%p",cp); |
d855497e MI |
93 | if (pvr2_ioread_init(cp) < 0) { |
94 | kfree(cp); | |
a0fd1cb1 | 95 | return NULL; |
d855497e MI |
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); | |
a0fd1cb1 | 107 | cp->sync_key_ptr = NULL; |
d855497e MI |
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); | |
a0fd1cb1 | 126 | cp->sync_key_ptr = NULL; |
d855497e MI |
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); | |
a0fd1cb1 MI |
146 | cp->c_buf = NULL; |
147 | cp->c_data_ptr = NULL; | |
d855497e MI |
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); | |
5fa1247a | 168 | while ((bp = pvr2_stream_get_idle_buffer(cp->stream)) != NULL) { |
d855497e MI |
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; | |
a0fd1cb1 MI |
181 | cp->c_buf = NULL; |
182 | cp->c_data_ptr = NULL; | |
d855497e MI |
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); do { | |
209 | if (cp->stream) { | |
210 | pvr2_trace(PVR2_TRACE_START_STOP, | |
211 | "/*---TRACE_READ---*/" | |
212 | " pvr2_ioread_setup (tear-down) id=%p",cp); | |
213 | pvr2_ioread_stop(cp); | |
214 | pvr2_stream_kill(cp->stream); | |
e61b6fc5 MI |
215 | if (pvr2_stream_get_buffer_count(cp->stream)) { |
216 | pvr2_stream_set_buffer_count(cp->stream,0); | |
217 | } | |
a0fd1cb1 | 218 | cp->stream = NULL; |
d855497e MI |
219 | } |
220 | if (sp) { | |
221 | pvr2_trace(PVR2_TRACE_START_STOP, | |
222 | "/*---TRACE_READ---*/" | |
223 | " pvr2_ioread_setup (setup) id=%p",cp); | |
224 | pvr2_stream_kill(sp); | |
225 | ret = pvr2_stream_set_buffer_count(sp,BUFFER_COUNT); | |
226 | if (ret < 0) return ret; | |
227 | for (idx = 0; idx < BUFFER_COUNT; idx++) { | |
228 | bp = pvr2_stream_get_buffer(sp,idx); | |
229 | pvr2_buffer_set_buffer(bp, | |
230 | cp->buffer_storage[idx], | |
231 | BUFFER_SIZE); | |
232 | } | |
233 | cp->stream = sp; | |
234 | } | |
235 | } while (0); mutex_unlock(&cp->mutex); | |
236 | ||
237 | return 0; | |
238 | } | |
239 | ||
240 | int pvr2_ioread_set_enabled(struct pvr2_ioread *cp,int fl) | |
241 | { | |
242 | int ret = 0; | |
243 | if ((!fl) == (!(cp->enabled))) return ret; | |
244 | ||
245 | mutex_lock(&cp->mutex); do { | |
246 | if (fl) { | |
247 | ret = pvr2_ioread_start(cp); | |
248 | } else { | |
249 | pvr2_ioread_stop(cp); | |
250 | } | |
251 | } while (0); mutex_unlock(&cp->mutex); | |
252 | return ret; | |
253 | } | |
254 | ||
07e337ee | 255 | static int pvr2_ioread_get_buffer(struct pvr2_ioread *cp) |
d855497e MI |
256 | { |
257 | int stat; | |
258 | ||
259 | while (cp->c_data_len <= cp->c_data_offs) { | |
260 | if (cp->c_buf) { | |
261 | // Flush out current buffer first. | |
262 | stat = pvr2_buffer_queue(cp->c_buf); | |
263 | if (stat < 0) { | |
264 | // Streaming error... | |
265 | pvr2_trace(PVR2_TRACE_DATA_FLOW, | |
266 | "/*---TRACE_READ---*/" | |
267 | " pvr2_ioread_read id=%p" | |
268 | " queue_error=%d", | |
269 | cp,stat); | |
270 | pvr2_ioread_stop(cp); | |
271 | return 0; | |
272 | } | |
a0fd1cb1 MI |
273 | cp->c_buf = NULL; |
274 | cp->c_data_ptr = NULL; | |
d855497e MI |
275 | cp->c_data_len = 0; |
276 | cp->c_data_offs = 0; | |
277 | } | |
278 | // Now get a freshly filled buffer. | |
279 | cp->c_buf = pvr2_stream_get_ready_buffer(cp->stream); | |
280 | if (!cp->c_buf) break; // Nothing ready; done. | |
281 | cp->c_data_len = pvr2_buffer_get_count(cp->c_buf); | |
282 | if (!cp->c_data_len) { | |
283 | // Nothing transferred. Was there an error? | |
284 | stat = pvr2_buffer_get_status(cp->c_buf); | |
285 | if (stat < 0) { | |
286 | // Streaming error... | |
287 | pvr2_trace(PVR2_TRACE_DATA_FLOW, | |
288 | "/*---TRACE_READ---*/" | |
289 | " pvr2_ioread_read id=%p" | |
290 | " buffer_error=%d", | |
291 | cp,stat); | |
292 | pvr2_ioread_stop(cp); | |
293 | // Give up. | |
294 | return 0; | |
295 | } | |
296 | // Start over... | |
297 | continue; | |
298 | } | |
299 | cp->c_data_offs = 0; | |
300 | cp->c_data_ptr = cp->buffer_storage[ | |
301 | pvr2_buffer_get_id(cp->c_buf)]; | |
302 | } | |
303 | return !0; | |
304 | } | |
305 | ||
07e337ee | 306 | static void pvr2_ioread_filter(struct pvr2_ioread *cp) |
d855497e MI |
307 | { |
308 | unsigned int idx; | |
309 | if (!cp->enabled) return; | |
310 | if (cp->sync_state != 1) return; | |
311 | ||
312 | // Search the stream for our synchronization key. This is made | |
313 | // complicated by the fact that in order to be honest with | |
314 | // ourselves here we must search across buffer boundaries... | |
315 | mutex_lock(&cp->mutex); while (1) { | |
316 | // Ensure we have a buffer | |
317 | if (!pvr2_ioread_get_buffer(cp)) break; | |
318 | if (!cp->c_data_len) break; | |
319 | ||
320 | // Now walk the buffer contents until we match the key or | |
321 | // run out of buffer data. | |
322 | for (idx = cp->c_data_offs; idx < cp->c_data_len; idx++) { | |
323 | if (cp->sync_buf_offs >= cp->sync_key_len) break; | |
324 | if (cp->c_data_ptr[idx] == | |
325 | cp->sync_key_ptr[cp->sync_buf_offs]) { | |
326 | // Found the next key byte | |
327 | (cp->sync_buf_offs)++; | |
328 | } else { | |
329 | // Whoops, mismatched. Start key over... | |
330 | cp->sync_buf_offs = 0; | |
331 | } | |
332 | } | |
333 | ||
334 | // Consume what we've walked through | |
335 | cp->c_data_offs += idx; | |
336 | cp->sync_trashed_count += idx; | |
337 | ||
338 | // If we've found the key, then update state and get out. | |
339 | if (cp->sync_buf_offs >= cp->sync_key_len) { | |
340 | cp->sync_trashed_count -= cp->sync_key_len; | |
341 | pvr2_trace(PVR2_TRACE_DATA_FLOW, | |
342 | "/*---TRACE_READ---*/" | |
343 | " sync_state <== 2 (skipped %u bytes)", | |
344 | cp->sync_trashed_count); | |
345 | cp->sync_state = 2; | |
346 | cp->sync_buf_offs = 0; | |
347 | break; | |
348 | } | |
349 | ||
350 | if (cp->c_data_offs < cp->c_data_len) { | |
351 | // Sanity check - should NEVER get here | |
352 | pvr2_trace(PVR2_TRACE_ERROR_LEGS, | |
353 | "ERROR: pvr2_ioread filter sync problem" | |
354 | " 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 | } mutex_unlock(&cp->mutex); | |
363 | } | |
364 | ||
365 | int pvr2_ioread_avail(struct pvr2_ioread *cp) | |
366 | { | |
367 | int ret; | |
368 | if (!(cp->enabled)) { | |
369 | // Stream is not enabled; so this is an I/O error | |
370 | return -EIO; | |
371 | } | |
372 | ||
373 | if (cp->sync_state == 1) { | |
374 | pvr2_ioread_filter(cp); | |
375 | if (cp->sync_state == 1) return -EAGAIN; | |
376 | } | |
377 | ||
378 | ret = 0; | |
379 | if (cp->stream_running) { | |
380 | if (!pvr2_stream_get_ready_count(cp->stream)) { | |
381 | // No data available at all right now. | |
382 | ret = -EAGAIN; | |
383 | } | |
384 | } else { | |
385 | if (pvr2_stream_get_ready_count(cp->stream) < BUFFER_COUNT/2) { | |
386 | // Haven't buffered up enough yet; try again later | |
387 | ret = -EAGAIN; | |
388 | } | |
389 | } | |
390 | ||
391 | if ((!(cp->spigot_open)) != (!(ret == 0))) { | |
392 | cp->spigot_open = (ret == 0); | |
393 | pvr2_trace(PVR2_TRACE_DATA_FLOW, | |
394 | "/*---TRACE_READ---*/ data is %s", | |
395 | cp->spigot_open ? "available" : "pending"); | |
396 | } | |
397 | ||
398 | return ret; | |
399 | } | |
400 | ||
401 | int pvr2_ioread_read(struct pvr2_ioread *cp,void __user *buf,unsigned int cnt) | |
402 | { | |
403 | unsigned int copied_cnt; | |
404 | unsigned int bcnt; | |
405 | const char *src; | |
406 | int stat; | |
407 | int ret = 0; | |
408 | unsigned int req_cnt = cnt; | |
409 | ||
410 | if (!cnt) { | |
411 | pvr2_trace(PVR2_TRACE_TRAP, | |
412 | "/*---TRACE_READ---*/ pvr2_ioread_read id=%p" | |
413 | " ZERO Request? Returning zero.",cp); | |
414 | return 0; | |
415 | } | |
416 | ||
417 | stat = pvr2_ioread_avail(cp); | |
418 | if (stat < 0) return stat; | |
419 | ||
420 | cp->stream_running = !0; | |
421 | ||
422 | mutex_lock(&cp->mutex); do { | |
423 | ||
424 | // Suck data out of the buffers and copy to the user | |
425 | copied_cnt = 0; | |
426 | if (!buf) cnt = 0; | |
427 | while (1) { | |
428 | if (!pvr2_ioread_get_buffer(cp)) { | |
429 | ret = -EIO; | |
430 | break; | |
431 | } | |
432 | ||
433 | if (!cnt) break; | |
434 | ||
435 | if (cp->sync_state == 2) { | |
436 | // We're repeating the sync key data into | |
437 | // the stream. | |
438 | src = cp->sync_key_ptr + cp->sync_buf_offs; | |
439 | bcnt = cp->sync_key_len - cp->sync_buf_offs; | |
440 | } else { | |
441 | // Normal buffer copy | |
442 | src = cp->c_data_ptr + cp->c_data_offs; | |
443 | bcnt = cp->c_data_len - cp->c_data_offs; | |
444 | } | |
445 | ||
446 | if (!bcnt) break; | |
447 | ||
448 | // Don't run past user's buffer | |
449 | if (bcnt > cnt) bcnt = cnt; | |
450 | ||
451 | if (copy_to_user(buf,src,bcnt)) { | |
452 | // User supplied a bad pointer? | |
453 | // Give up - this *will* cause data | |
454 | // to be lost. | |
455 | ret = -EFAULT; | |
456 | break; | |
457 | } | |
458 | cnt -= bcnt; | |
459 | buf += bcnt; | |
460 | copied_cnt += bcnt; | |
461 | ||
462 | if (cp->sync_state == 2) { | |
463 | // Update offset inside sync key that we're | |
464 | // repeating back out. | |
465 | cp->sync_buf_offs += bcnt; | |
466 | if (cp->sync_buf_offs >= cp->sync_key_len) { | |
467 | // Consumed entire key; switch mode | |
468 | // to normal. | |
469 | pvr2_trace(PVR2_TRACE_DATA_FLOW, | |
470 | "/*---TRACE_READ---*/" | |
471 | " sync_state <== 0"); | |
472 | cp->sync_state = 0; | |
473 | } | |
474 | } else { | |
475 | // Update buffer offset. | |
476 | cp->c_data_offs += bcnt; | |
477 | } | |
478 | } | |
479 | ||
480 | } while (0); mutex_unlock(&cp->mutex); | |
481 | ||
482 | if (!ret) { | |
483 | if (copied_cnt) { | |
484 | // If anything was copied, return that count | |
485 | ret = copied_cnt; | |
486 | } else { | |
487 | // Nothing copied; suggest to caller that another | |
488 | // attempt should be tried again later | |
489 | ret = -EAGAIN; | |
490 | } | |
491 | } | |
492 | ||
493 | pvr2_trace(PVR2_TRACE_DATA_FLOW, | |
494 | "/*---TRACE_READ---*/ pvr2_ioread_read" | |
495 | " id=%p request=%d result=%d", | |
496 | cp,req_cnt,ret); | |
497 | return ret; | |
498 | } | |
499 | ||
500 | ||
501 | /* | |
502 | Stuff for Emacs to see, in order to encourage consistent editing style: | |
503 | *** Local Variables: *** | |
504 | *** mode: c *** | |
505 | *** fill-column: 75 *** | |
506 | *** tab-width: 8 *** | |
507 | *** c-basic-offset: 8 *** | |
508 | *** End: *** | |
509 | */ |