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