Pull ec into test branch
[linux-2.6] / drivers / media / video / pvrusb2 / pvrusb2-ioread.c
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
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 = kmalloc(sizeof(*cp),GFP_KERNEL);
91         if (!cp) return NULL;
92         pvr2_trace(PVR2_TRACE_STRUCT,"pvr2_ioread_create id=%p",cp);
93         memset(cp,0,sizeof(*cp));
94         if (pvr2_ioread_init(cp) < 0) {
95                 kfree(cp);
96                 return NULL;
97         }
98         return cp;
99 }
100
101 void pvr2_ioread_destroy(struct pvr2_ioread *cp)
102 {
103         if (!cp) return;
104         pvr2_ioread_done(cp);
105         pvr2_trace(PVR2_TRACE_STRUCT,"pvr2_ioread_destroy id=%p",cp);
106         if (cp->sync_key_ptr) {
107                 kfree(cp->sync_key_ptr);
108                 cp->sync_key_ptr = NULL;
109         }
110         kfree(cp);
111 }
112
113 void pvr2_ioread_set_sync_key(struct pvr2_ioread *cp,
114                               const char *sync_key_ptr,
115                               unsigned int sync_key_len)
116 {
117         if (!cp) return;
118
119         if (!sync_key_ptr) sync_key_len = 0;
120         if ((sync_key_len == cp->sync_key_len) &&
121             ((!sync_key_len) ||
122              (!memcmp(sync_key_ptr,cp->sync_key_ptr,sync_key_len)))) return;
123
124         if (sync_key_len != cp->sync_key_len) {
125                 if (cp->sync_key_ptr) {
126                         kfree(cp->sync_key_ptr);
127                         cp->sync_key_ptr = NULL;
128                 }
129                 cp->sync_key_len = 0;
130                 if (sync_key_len) {
131                         cp->sync_key_ptr = kmalloc(sync_key_len,GFP_KERNEL);
132                         if (cp->sync_key_ptr) {
133                                 cp->sync_key_len = sync_key_len;
134                         }
135                 }
136         }
137         if (!cp->sync_key_len) return;
138         memcpy(cp->sync_key_ptr,sync_key_ptr,cp->sync_key_len);
139 }
140
141 static void pvr2_ioread_stop(struct pvr2_ioread *cp)
142 {
143         if (!(cp->enabled)) return;
144         pvr2_trace(PVR2_TRACE_START_STOP,
145                    "/*---TRACE_READ---*/ pvr2_ioread_stop id=%p",cp);
146         pvr2_stream_kill(cp->stream);
147         cp->c_buf = NULL;
148         cp->c_data_ptr = NULL;
149         cp->c_data_len = 0;
150         cp->c_data_offs = 0;
151         cp->enabled = 0;
152         cp->stream_running = 0;
153         cp->spigot_open = 0;
154         if (cp->sync_state) {
155                 pvr2_trace(PVR2_TRACE_DATA_FLOW,
156                            "/*---TRACE_READ---*/ sync_state <== 0");
157                 cp->sync_state = 0;
158         }
159 }
160
161 static int pvr2_ioread_start(struct pvr2_ioread *cp)
162 {
163         int stat;
164         struct pvr2_buffer *bp;
165         if (cp->enabled) return 0;
166         if (!(cp->stream)) return 0;
167         pvr2_trace(PVR2_TRACE_START_STOP,
168                    "/*---TRACE_READ---*/ pvr2_ioread_start id=%p",cp);
169         while ((bp = pvr2_stream_get_idle_buffer(cp->stream)) != 0) {
170                 stat = pvr2_buffer_queue(bp);
171                 if (stat < 0) {
172                         pvr2_trace(PVR2_TRACE_DATA_FLOW,
173                                    "/*---TRACE_READ---*/"
174                                    " pvr2_ioread_start id=%p"
175                                    " error=%d",
176                                    cp,stat);
177                         pvr2_ioread_stop(cp);
178                         return stat;
179                 }
180         }
181         cp->enabled = !0;
182         cp->c_buf = NULL;
183         cp->c_data_ptr = NULL;
184         cp->c_data_len = 0;
185         cp->c_data_offs = 0;
186         cp->stream_running = 0;
187         if (cp->sync_key_len) {
188                 pvr2_trace(PVR2_TRACE_DATA_FLOW,
189                            "/*---TRACE_READ---*/ sync_state <== 1");
190                 cp->sync_state = 1;
191                 cp->sync_trashed_count = 0;
192                 cp->sync_buf_offs = 0;
193         }
194         cp->spigot_open = 0;
195         return 0;
196 }
197
198 struct pvr2_stream *pvr2_ioread_get_stream(struct pvr2_ioread *cp)
199 {
200         return cp->stream;
201 }
202
203 int pvr2_ioread_setup(struct pvr2_ioread *cp,struct pvr2_stream *sp)
204 {
205         int ret;
206         unsigned int idx;
207         struct pvr2_buffer *bp;
208
209         mutex_lock(&cp->mutex); 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) return ret;
228                         for (idx = 0; idx < BUFFER_COUNT; idx++) {
229                                 bp = pvr2_stream_get_buffer(sp,idx);
230                                 pvr2_buffer_set_buffer(bp,
231                                                        cp->buffer_storage[idx],
232                                                        BUFFER_SIZE);
233                         }
234                         cp->stream = sp;
235                 }
236         } while (0); mutex_unlock(&cp->mutex);
237
238         return 0;
239 }
240
241 int pvr2_ioread_set_enabled(struct pvr2_ioread *cp,int fl)
242 {
243         int ret = 0;
244         if ((!fl) == (!(cp->enabled))) return ret;
245
246         mutex_lock(&cp->mutex); do {
247                 if (fl) {
248                         ret = pvr2_ioread_start(cp);
249                 } else {
250                         pvr2_ioread_stop(cp);
251                 }
252         } while (0); mutex_unlock(&cp->mutex);
253         return ret;
254 }
255
256 static int pvr2_ioread_get_buffer(struct pvr2_ioread *cp)
257 {
258         int stat;
259
260         while (cp->c_data_len <= cp->c_data_offs) {
261                 if (cp->c_buf) {
262                         // Flush out current buffer first.
263                         stat = pvr2_buffer_queue(cp->c_buf);
264                         if (stat < 0) {
265                                 // Streaming error...
266                                 pvr2_trace(PVR2_TRACE_DATA_FLOW,
267                                            "/*---TRACE_READ---*/"
268                                            " pvr2_ioread_read id=%p"
269                                            " queue_error=%d",
270                                            cp,stat);
271                                 pvr2_ioread_stop(cp);
272                                 return 0;
273                         }
274                         cp->c_buf = NULL;
275                         cp->c_data_ptr = NULL;
276                         cp->c_data_len = 0;
277                         cp->c_data_offs = 0;
278                 }
279                 // Now get a freshly filled buffer.
280                 cp->c_buf = pvr2_stream_get_ready_buffer(cp->stream);
281                 if (!cp->c_buf) break; // Nothing ready; done.
282                 cp->c_data_len = pvr2_buffer_get_count(cp->c_buf);
283                 if (!cp->c_data_len) {
284                         // Nothing transferred.  Was there an error?
285                         stat = pvr2_buffer_get_status(cp->c_buf);
286                         if (stat < 0) {
287                                 // Streaming error...
288                                 pvr2_trace(PVR2_TRACE_DATA_FLOW,
289                                            "/*---TRACE_READ---*/"
290                                            " pvr2_ioread_read id=%p"
291                                            " 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); while (1) {
317                 // Ensure we have a buffer
318                 if (!pvr2_ioread_get_buffer(cp)) break;
319                 if (!cp->c_data_len) break;
320
321                 // Now walk the buffer contents until we match the key or
322                 // run out of buffer data.
323                 for (idx = cp->c_data_offs; idx < cp->c_data_len; idx++) {
324                         if (cp->sync_buf_offs >= cp->sync_key_len) break;
325                         if (cp->c_data_ptr[idx] ==
326                             cp->sync_key_ptr[cp->sync_buf_offs]) {
327                                 // Found the next key byte
328                                 (cp->sync_buf_offs)++;
329                         } else {
330                                 // Whoops, mismatched.  Start key over...
331                                 cp->sync_buf_offs = 0;
332                         }
333                 }
334
335                 // Consume what we've walked through
336                 cp->c_data_offs += idx;
337                 cp->sync_trashed_count += idx;
338
339                 // If we've found the key, then update state and get out.
340                 if (cp->sync_buf_offs >= cp->sync_key_len) {
341                         cp->sync_trashed_count -= cp->sync_key_len;
342                         pvr2_trace(PVR2_TRACE_DATA_FLOW,
343                                    "/*---TRACE_READ---*/"
344                                    " 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"
355                                    " len=%u offs=%u",
356                                    cp->c_data_len,cp->c_data_offs);
357                         // Get out so we don't get stuck in an infinite
358                         // loop.
359                         break;
360                 }
361
362                 continue; // (for clarity)
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"
414                            " ZERO Request? Returning zero.",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); do {
424
425                 // Suck data out of the buffers and copy to the user
426                 copied_cnt = 0;
427                 if (!buf) cnt = 0;
428                 while (1) {
429                         if (!pvr2_ioread_get_buffer(cp)) {
430                                 ret = -EIO;
431                                 break;
432                         }
433
434                         if (!cnt) break;
435
436                         if (cp->sync_state == 2) {
437                                 // We're repeating the sync key data into
438                                 // the stream.
439                                 src = cp->sync_key_ptr + cp->sync_buf_offs;
440                                 bcnt = cp->sync_key_len - cp->sync_buf_offs;
441                         } else {
442                                 // Normal buffer copy
443                                 src = cp->c_data_ptr + cp->c_data_offs;
444                                 bcnt = cp->c_data_len - cp->c_data_offs;
445                         }
446
447                         if (!bcnt) break;
448
449                         // Don't run past user's buffer
450                         if (bcnt > cnt) bcnt = cnt;
451
452                         if (copy_to_user(buf,src,bcnt)) {
453                                 // User supplied a bad pointer?
454                                 // Give up - this *will* cause data
455                                 // to be lost.
456                                 ret = -EFAULT;
457                                 break;
458                         }
459                         cnt -= bcnt;
460                         buf += bcnt;
461                         copied_cnt += bcnt;
462
463                         if (cp->sync_state == 2) {
464                                 // Update offset inside sync key that we're
465                                 // repeating back out.
466                                 cp->sync_buf_offs += bcnt;
467                                 if (cp->sync_buf_offs >= cp->sync_key_len) {
468                                         // Consumed entire key; switch mode
469                                         // to normal.
470                                         pvr2_trace(PVR2_TRACE_DATA_FLOW,
471                                                    "/*---TRACE_READ---*/"
472                                                    " 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); mutex_unlock(&cp->mutex);
482
483         if (!ret) {
484                 if (copied_cnt) {
485                         // If anything was copied, return that count
486                         ret = copied_cnt;
487                 } else {
488                         // Nothing copied; suggest to caller that another
489                         // attempt should be tried again later
490                         ret = -EAGAIN;
491                 }
492         }
493
494         pvr2_trace(PVR2_TRACE_DATA_FLOW,
495                    "/*---TRACE_READ---*/ pvr2_ioread_read"
496                    " id=%p request=%d result=%d",
497                    cp,req_cnt,ret);
498         return ret;
499 }
500
501
502 /*
503   Stuff for Emacs to see, in order to encourage consistent editing style:
504   *** Local Variables: ***
505   *** mode: c ***
506   *** fill-column: 75 ***
507   *** tab-width: 8 ***
508   *** c-basic-offset: 8 ***
509   *** End: ***
510   */