5 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
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
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.
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
22 #include "pvrusb2-io.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>
29 static const char *pvr2_buffer_state_decode(enum pvr2_buffer_state);
31 #define BUFFER_SIG 0x47653271
33 // #define SANITY_CHECK_BUFFERS
36 #ifdef SANITY_CHECK_BUFFERS
37 #define BUFFER_CHECK(bp) do { \
38 if ((bp)->signature != BUFFER_SIG) { \
39 pvr2_trace(PVR2_TRACE_ERROR_LEGS, \
40 "Buffer %p is bad at %s:%d", \
41 (bp),__FILE__,__LINE__); \
42 pvr2_buffer_describe(bp,"BadSig"); \
47 #define BUFFER_CHECK(bp) do {} while(0)
51 /* Buffers queued for reading */
52 struct list_head queued_list;
54 unsigned int q_bcount;
55 /* Buffers with retrieved data */
56 struct list_head ready_list;
58 unsigned int r_bcount;
59 /* Buffers available for use */
60 struct list_head idle_list;
62 unsigned int i_bcount;
63 /* Pointers to all buffers */
64 struct pvr2_buffer **buffers;
65 /* Array size of buffers */
66 unsigned int buffer_slot_count;
67 /* Total buffers actually in circulation */
68 unsigned int buffer_total_count;
69 /* Designed number of buffers to be in circulation */
70 unsigned int buffer_target_count;
71 /* Executed when ready list become non-empty */
72 pvr2_stream_callback callback_func;
74 /* Context for transfer endpoint */
75 struct usb_device *dev;
77 /* Overhead for mutex enforcement */
80 /* Tracking state for tolerating errors */
81 unsigned int fail_count;
82 unsigned int fail_tolerance;
84 unsigned int buffers_processed;
85 unsigned int buffers_failed;
86 unsigned int bytes_processed;
92 enum pvr2_buffer_state state;
93 void *ptr; /* Pointer to storage area */
94 unsigned int max_count; /* Size of storage area */
95 unsigned int used_count; /* Amount of valid data in storage area */
96 int status; /* Transfer result status */
97 struct pvr2_stream *stream;
98 struct list_head list_overhead;
102 static const char *pvr2_buffer_state_decode(enum pvr2_buffer_state st)
105 case pvr2_buffer_state_none: return "none";
106 case pvr2_buffer_state_idle: return "idle";
107 case pvr2_buffer_state_queued: return "queued";
108 case pvr2_buffer_state_ready: return "ready";
113 #ifdef SANITY_CHECK_BUFFERS
114 static void pvr2_buffer_describe(struct pvr2_buffer *bp,const char *msg)
116 pvr2_trace(PVR2_TRACE_INFO,
117 "buffer%s%s %p state=%s id=%d status=%d"
118 " stream=%p purb=%p sig=0x%x",
122 (bp ? pvr2_buffer_state_decode(bp->state) : "(invalid)"),
124 (bp ? bp->status : 0),
125 (bp ? bp->stream : NULL),
126 (bp ? bp->purb : NULL),
127 (bp ? bp->signature : 0));
129 #endif /* SANITY_CHECK_BUFFERS */
131 static void pvr2_buffer_remove(struct pvr2_buffer *bp)
136 struct pvr2_stream *sp = bp->stream;
138 case pvr2_buffer_state_idle:
140 bcnt = &sp->i_bcount;
141 ccnt = bp->max_count;
143 case pvr2_buffer_state_queued:
145 bcnt = &sp->q_bcount;
146 ccnt = bp->max_count;
148 case pvr2_buffer_state_ready:
150 bcnt = &sp->r_bcount;
151 ccnt = bp->used_count;
156 list_del_init(&bp->list_overhead);
159 pvr2_trace(PVR2_TRACE_BUF_FLOW,
160 "/*---TRACE_FLOW---*/"
161 " bufferPool %8s dec cap=%07d cnt=%02d",
162 pvr2_buffer_state_decode(bp->state),*bcnt,*cnt);
163 bp->state = pvr2_buffer_state_none;
166 static void pvr2_buffer_set_none(struct pvr2_buffer *bp)
168 unsigned long irq_flags;
169 struct pvr2_stream *sp;
172 pvr2_trace(PVR2_TRACE_BUF_FLOW,
173 "/*---TRACE_FLOW---*/ bufferState %p %6s --> %6s",
175 pvr2_buffer_state_decode(bp->state),
176 pvr2_buffer_state_decode(pvr2_buffer_state_none));
177 spin_lock_irqsave(&sp->list_lock,irq_flags);
178 pvr2_buffer_remove(bp);
179 spin_unlock_irqrestore(&sp->list_lock,irq_flags);
182 static int pvr2_buffer_set_ready(struct pvr2_buffer *bp)
185 unsigned long irq_flags;
186 struct pvr2_stream *sp;
189 pvr2_trace(PVR2_TRACE_BUF_FLOW,
190 "/*---TRACE_FLOW---*/ bufferState %p %6s --> %6s",
192 pvr2_buffer_state_decode(bp->state),
193 pvr2_buffer_state_decode(pvr2_buffer_state_ready));
194 spin_lock_irqsave(&sp->list_lock,irq_flags);
195 fl = (sp->r_count == 0);
196 pvr2_buffer_remove(bp);
197 list_add_tail(&bp->list_overhead,&sp->ready_list);
198 bp->state = pvr2_buffer_state_ready;
200 sp->r_bcount += bp->used_count;
201 pvr2_trace(PVR2_TRACE_BUF_FLOW,
202 "/*---TRACE_FLOW---*/"
203 " bufferPool %8s inc cap=%07d cnt=%02d",
204 pvr2_buffer_state_decode(bp->state),
205 sp->r_bcount,sp->r_count);
206 spin_unlock_irqrestore(&sp->list_lock,irq_flags);
210 static void pvr2_buffer_set_idle(struct pvr2_buffer *bp)
212 unsigned long irq_flags;
213 struct pvr2_stream *sp;
216 pvr2_trace(PVR2_TRACE_BUF_FLOW,
217 "/*---TRACE_FLOW---*/ bufferState %p %6s --> %6s",
219 pvr2_buffer_state_decode(bp->state),
220 pvr2_buffer_state_decode(pvr2_buffer_state_idle));
221 spin_lock_irqsave(&sp->list_lock,irq_flags);
222 pvr2_buffer_remove(bp);
223 list_add_tail(&bp->list_overhead,&sp->idle_list);
224 bp->state = pvr2_buffer_state_idle;
226 sp->i_bcount += bp->max_count;
227 pvr2_trace(PVR2_TRACE_BUF_FLOW,
228 "/*---TRACE_FLOW---*/"
229 " bufferPool %8s inc cap=%07d cnt=%02d",
230 pvr2_buffer_state_decode(bp->state),
231 sp->i_bcount,sp->i_count);
232 spin_unlock_irqrestore(&sp->list_lock,irq_flags);
235 static void pvr2_buffer_set_queued(struct pvr2_buffer *bp)
237 unsigned long irq_flags;
238 struct pvr2_stream *sp;
241 pvr2_trace(PVR2_TRACE_BUF_FLOW,
242 "/*---TRACE_FLOW---*/ bufferState %p %6s --> %6s",
244 pvr2_buffer_state_decode(bp->state),
245 pvr2_buffer_state_decode(pvr2_buffer_state_queued));
246 spin_lock_irqsave(&sp->list_lock,irq_flags);
247 pvr2_buffer_remove(bp);
248 list_add_tail(&bp->list_overhead,&sp->queued_list);
249 bp->state = pvr2_buffer_state_queued;
251 sp->q_bcount += bp->max_count;
252 pvr2_trace(PVR2_TRACE_BUF_FLOW,
253 "/*---TRACE_FLOW---*/"
254 " bufferPool %8s inc cap=%07d cnt=%02d",
255 pvr2_buffer_state_decode(bp->state),
256 sp->q_bcount,sp->q_count);
257 spin_unlock_irqrestore(&sp->list_lock,irq_flags);
260 static void pvr2_buffer_wipe(struct pvr2_buffer *bp)
262 if (bp->state == pvr2_buffer_state_queued) {
263 usb_kill_urb(bp->purb);
267 static int pvr2_buffer_init(struct pvr2_buffer *bp,
268 struct pvr2_stream *sp,
271 memset(bp,0,sizeof(*bp));
272 bp->signature = BUFFER_SIG;
274 pvr2_trace(PVR2_TRACE_BUF_POOL,
275 "/*---TRACE_FLOW---*/ bufferInit %p stream=%p",bp,sp);
277 bp->state = pvr2_buffer_state_none;
278 INIT_LIST_HEAD(&bp->list_overhead);
279 bp->purb = usb_alloc_urb(0,GFP_KERNEL);
280 if (! bp->purb) return -ENOMEM;
281 #ifdef SANITY_CHECK_BUFFERS
282 pvr2_buffer_describe(bp,"create");
287 static void pvr2_buffer_done(struct pvr2_buffer *bp)
289 #ifdef SANITY_CHECK_BUFFERS
290 pvr2_buffer_describe(bp,"delete");
292 pvr2_buffer_wipe(bp);
293 pvr2_buffer_set_none(bp);
296 usb_free_urb(bp->purb);
297 pvr2_trace(PVR2_TRACE_BUF_POOL,"/*---TRACE_FLOW---*/"
298 " bufferDone %p",bp);
301 static int pvr2_stream_buffer_count(struct pvr2_stream *sp,unsigned int cnt)
306 /* Allocate buffers pointer array in multiples of 32 entries */
307 if (cnt == sp->buffer_total_count) return 0;
309 pvr2_trace(PVR2_TRACE_BUF_POOL,
310 "/*---TRACE_FLOW---*/ poolResize "
311 " stream=%p cur=%d adj=%+d",
313 sp->buffer_total_count,
314 cnt-sp->buffer_total_count);
317 if (cnt > scnt) scnt += 0x20;
319 if (cnt > sp->buffer_total_count) {
320 if (scnt > sp->buffer_slot_count) {
321 struct pvr2_buffer **nb;
322 nb = kmalloc(scnt * sizeof(*nb),GFP_KERNEL);
323 if (!nb) return -ENOMEM;
324 if (sp->buffer_slot_count) {
325 memcpy(nb,sp->buffers,
326 sp->buffer_slot_count * sizeof(*nb));
330 sp->buffer_slot_count = scnt;
332 while (sp->buffer_total_count < cnt) {
333 struct pvr2_buffer *bp;
334 bp = kmalloc(sizeof(*bp),GFP_KERNEL);
335 if (!bp) return -ENOMEM;
336 ret = pvr2_buffer_init(bp,sp,sp->buffer_total_count);
341 sp->buffers[sp->buffer_total_count] = bp;
342 (sp->buffer_total_count)++;
343 pvr2_buffer_set_idle(bp);
346 while (sp->buffer_total_count > cnt) {
347 struct pvr2_buffer *bp;
348 bp = sp->buffers[sp->buffer_total_count - 1];
350 sp->buffers[sp->buffer_total_count - 1] = NULL;
351 (sp->buffer_total_count)--;
352 pvr2_buffer_done(bp);
355 if (scnt < sp->buffer_slot_count) {
356 struct pvr2_buffer **nb = NULL;
358 nb = kmalloc(scnt * sizeof(*nb),GFP_KERNEL);
359 if (!nb) return -ENOMEM;
360 memcpy(nb,sp->buffers,scnt * sizeof(*nb));
364 sp->buffer_slot_count = scnt;
370 static int pvr2_stream_achieve_buffer_count(struct pvr2_stream *sp)
372 struct pvr2_buffer *bp;
375 if (sp->buffer_total_count == sp->buffer_target_count) return 0;
377 pvr2_trace(PVR2_TRACE_BUF_POOL,
378 "/*---TRACE_FLOW---*/"
379 " poolCheck stream=%p cur=%d tgt=%d",
380 sp,sp->buffer_total_count,sp->buffer_target_count);
382 if (sp->buffer_total_count < sp->buffer_target_count) {
383 return pvr2_stream_buffer_count(sp,sp->buffer_target_count);
387 while ((sp->buffer_total_count - cnt) > sp->buffer_target_count) {
388 bp = sp->buffers[sp->buffer_total_count - (cnt + 1)];
389 if (bp->state != pvr2_buffer_state_idle) break;
393 pvr2_stream_buffer_count(sp,sp->buffer_total_count - cnt);
399 static void pvr2_stream_internal_flush(struct pvr2_stream *sp)
401 struct list_head *lp;
402 struct pvr2_buffer *bp1;
403 while ((lp = sp->queued_list.next) != &sp->queued_list) {
404 bp1 = list_entry(lp,struct pvr2_buffer,list_overhead);
405 pvr2_buffer_wipe(bp1);
406 /* At this point, we should be guaranteed that no
407 completion callback may happen on this buffer. But it's
408 possible that it might have completed after we noticed
409 it but before we wiped it. So double check its status
411 if (bp1->state != pvr2_buffer_state_queued) continue;
412 pvr2_buffer_set_idle(bp1);
414 if (sp->buffer_total_count != sp->buffer_target_count) {
415 pvr2_stream_achieve_buffer_count(sp);
419 static void pvr2_stream_init(struct pvr2_stream *sp)
421 spin_lock_init(&sp->list_lock);
422 mutex_init(&sp->mutex);
423 INIT_LIST_HEAD(&sp->queued_list);
424 INIT_LIST_HEAD(&sp->ready_list);
425 INIT_LIST_HEAD(&sp->idle_list);
428 static void pvr2_stream_done(struct pvr2_stream *sp)
430 mutex_lock(&sp->mutex); do {
431 pvr2_stream_internal_flush(sp);
432 pvr2_stream_buffer_count(sp,0);
433 } while (0); mutex_unlock(&sp->mutex);
436 static void buffer_complete(struct urb *urb)
438 struct pvr2_buffer *bp = urb->context;
439 struct pvr2_stream *sp;
440 unsigned long irq_flags;
445 pvr2_trace(PVR2_TRACE_BUF_FLOW,
446 "/*---TRACE_FLOW---*/ bufferComplete %p stat=%d cnt=%d",
447 bp,urb->status,urb->actual_length);
448 spin_lock_irqsave(&sp->list_lock,irq_flags);
449 if ((!(urb->status)) ||
450 (urb->status == -ENOENT) ||
451 (urb->status == -ECONNRESET) ||
452 (urb->status == -ESHUTDOWN)) {
453 (sp->buffers_processed)++;
454 sp->bytes_processed += urb->actual_length;
455 bp->used_count = urb->actual_length;
456 if (sp->fail_count) {
457 pvr2_trace(PVR2_TRACE_TOLERANCE,
458 "stream %p transfer ok"
459 " - fail count reset",sp);
462 } else if (sp->fail_count < sp->fail_tolerance) {
463 // We can tolerate this error, because we're below the
466 (sp->buffers_failed)++;
467 pvr2_trace(PVR2_TRACE_TOLERANCE,
468 "stream %p ignoring error %d"
469 " - fail count increased to %u",
470 sp,urb->status,sp->fail_count);
472 (sp->buffers_failed)++;
473 bp->status = urb->status;
475 spin_unlock_irqrestore(&sp->list_lock,irq_flags);
476 pvr2_buffer_set_ready(bp);
477 if (sp && sp->callback_func) {
478 sp->callback_func(sp->callback_data);
482 struct pvr2_stream *pvr2_stream_create(void)
484 struct pvr2_stream *sp;
485 sp = kzalloc(sizeof(*sp),GFP_KERNEL);
487 pvr2_trace(PVR2_TRACE_INIT,"pvr2_stream_create: sp=%p",sp);
488 pvr2_stream_init(sp);
492 void pvr2_stream_destroy(struct pvr2_stream *sp)
495 pvr2_trace(PVR2_TRACE_INIT,"pvr2_stream_destroy: sp=%p",sp);
496 pvr2_stream_done(sp);
500 void pvr2_stream_setup(struct pvr2_stream *sp,
501 struct usb_device *dev,
503 unsigned int tolerance)
505 mutex_lock(&sp->mutex); do {
506 pvr2_stream_internal_flush(sp);
508 sp->endpoint = endpoint;
509 sp->fail_tolerance = tolerance;
510 } while(0); mutex_unlock(&sp->mutex);
513 void pvr2_stream_set_callback(struct pvr2_stream *sp,
514 pvr2_stream_callback func,
517 unsigned long irq_flags;
518 mutex_lock(&sp->mutex); do {
519 spin_lock_irqsave(&sp->list_lock,irq_flags);
520 sp->callback_data = data;
521 sp->callback_func = func;
522 spin_unlock_irqrestore(&sp->list_lock,irq_flags);
523 } while(0); mutex_unlock(&sp->mutex);
526 void pvr2_stream_get_stats(struct pvr2_stream *sp,
527 struct pvr2_stream_stats *stats,
530 unsigned long irq_flags;
531 spin_lock_irqsave(&sp->list_lock,irq_flags);
533 stats->buffers_in_queue = sp->q_count;
534 stats->buffers_in_idle = sp->i_count;
535 stats->buffers_in_ready = sp->r_count;
536 stats->buffers_processed = sp->buffers_processed;
537 stats->buffers_failed = sp->buffers_failed;
538 stats->bytes_processed = sp->bytes_processed;
541 sp->buffers_processed = 0;
542 sp->buffers_failed = 0;
543 sp->bytes_processed = 0;
545 spin_unlock_irqrestore(&sp->list_lock,irq_flags);
548 /* Query / set the nominal buffer count */
549 int pvr2_stream_get_buffer_count(struct pvr2_stream *sp)
551 return sp->buffer_target_count;
554 int pvr2_stream_set_buffer_count(struct pvr2_stream *sp,unsigned int cnt)
557 if (sp->buffer_target_count == cnt) return 0;
558 mutex_lock(&sp->mutex); do {
559 sp->buffer_target_count = cnt;
560 ret = pvr2_stream_achieve_buffer_count(sp);
561 } while(0); mutex_unlock(&sp->mutex);
565 struct pvr2_buffer *pvr2_stream_get_idle_buffer(struct pvr2_stream *sp)
567 struct list_head *lp = sp->idle_list.next;
568 if (lp == &sp->idle_list) return NULL;
569 return list_entry(lp,struct pvr2_buffer,list_overhead);
572 struct pvr2_buffer *pvr2_stream_get_ready_buffer(struct pvr2_stream *sp)
574 struct list_head *lp = sp->ready_list.next;
575 if (lp == &sp->ready_list) return NULL;
576 return list_entry(lp,struct pvr2_buffer,list_overhead);
579 struct pvr2_buffer *pvr2_stream_get_buffer(struct pvr2_stream *sp,int id)
581 if (id < 0) return NULL;
582 if (id >= sp->buffer_total_count) return NULL;
583 return sp->buffers[id];
586 int pvr2_stream_get_ready_count(struct pvr2_stream *sp)
591 void pvr2_stream_kill(struct pvr2_stream *sp)
593 struct pvr2_buffer *bp;
594 mutex_lock(&sp->mutex); do {
595 pvr2_stream_internal_flush(sp);
596 while ((bp = pvr2_stream_get_ready_buffer(sp)) != NULL) {
597 pvr2_buffer_set_idle(bp);
599 if (sp->buffer_total_count != sp->buffer_target_count) {
600 pvr2_stream_achieve_buffer_count(sp);
602 } while(0); mutex_unlock(&sp->mutex);
605 int pvr2_buffer_queue(struct pvr2_buffer *bp)
613 struct pvr2_stream *sp;
614 if (!bp) return -EINVAL;
616 mutex_lock(&sp->mutex); do {
617 pvr2_buffer_wipe(bp);
622 pvr2_buffer_set_queued(bp);
624 for (idx = 0; idx < (bp->max_count) / 4; idx++) {
627 ((unsigned int *)(bp->ptr))[idx] = val;
630 bp->status = -EINPROGRESS;
631 usb_fill_bulk_urb(bp->purb, // struct urb *urb
632 sp->dev, // struct usb_device *dev
634 usb_rcvbulkpipe(sp->dev,sp->endpoint),
635 bp->ptr, // void *transfer_buffer
636 bp->max_count, // int buffer_length
639 usb_submit_urb(bp->purb,GFP_KERNEL);
640 } while(0); mutex_unlock(&sp->mutex);
644 int pvr2_buffer_set_buffer(struct pvr2_buffer *bp,void *ptr,unsigned int cnt)
647 unsigned long irq_flags;
648 struct pvr2_stream *sp;
649 if (!bp) return -EINVAL;
651 mutex_lock(&sp->mutex); do {
652 spin_lock_irqsave(&sp->list_lock,irq_flags);
653 if (bp->state != pvr2_buffer_state_idle) {
657 bp->stream->i_bcount -= bp->max_count;
659 bp->stream->i_bcount += bp->max_count;
660 pvr2_trace(PVR2_TRACE_BUF_FLOW,
661 "/*---TRACE_FLOW---*/ bufferPool "
662 " %8s cap cap=%07d cnt=%02d",
663 pvr2_buffer_state_decode(
664 pvr2_buffer_state_idle),
665 bp->stream->i_bcount,bp->stream->i_count);
667 spin_unlock_irqrestore(&sp->list_lock,irq_flags);
668 } while(0); mutex_unlock(&sp->mutex);
672 unsigned int pvr2_buffer_get_count(struct pvr2_buffer *bp)
674 return bp->used_count;
677 int pvr2_buffer_get_status(struct pvr2_buffer *bp)
682 int pvr2_buffer_get_id(struct pvr2_buffer *bp)
689 Stuff for Emacs to see, in order to encourage consistent editing style:
690 *** Local Variables: ***
692 *** fill-column: 75 ***
694 *** c-basic-offset: 8 ***