4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
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
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.
17 #include "pvrusb2-io.h"
18 #include "pvrusb2-debug.h"
19 #include <linux/errno.h>
20 #include <linux/string.h>
21 #include <linux/slab.h>
22 #include <linux/mutex.h>
24 static const char *pvr2_buffer_state_decode(enum pvr2_buffer_state
);
26 #define BUFFER_SIG 0x47653271
28 // #define SANITY_CHECK_BUFFERS
31 #ifdef SANITY_CHECK_BUFFERS
32 #define BUFFER_CHECK(bp) do { \
33 if ((bp)->signature != BUFFER_SIG) { \
34 pvr2_trace(PVR2_TRACE_ERROR_LEGS, \
35 "Buffer %p is bad at %s:%d", \
36 (bp), __FILE__, __LINE__); \
37 pvr2_buffer_describe(bp, "BadSig"); \
42 #define BUFFER_CHECK(bp) do {} while (0)
46 /* Buffers queued for reading */
47 struct list_head queued_list
;
49 unsigned int q_bcount
;
50 /* Buffers with retrieved data */
51 struct list_head ready_list
;
53 unsigned int r_bcount
;
54 /* Buffers available for use */
55 struct list_head idle_list
;
57 unsigned int i_bcount
;
58 /* Pointers to all buffers */
59 struct pvr2_buffer
**buffers
;
60 /* Array size of buffers */
61 unsigned int buffer_slot_count
;
62 /* Total buffers actually in circulation */
63 unsigned int buffer_total_count
;
64 /* Designed number of buffers to be in circulation */
65 unsigned int buffer_target_count
;
66 /* Executed when ready list become non-empty */
67 pvr2_stream_callback callback_func
;
69 /* Context for transfer endpoint */
70 struct usb_device
*dev
;
72 /* Overhead for mutex enforcement */
75 /* Tracking state for tolerating errors */
76 unsigned int fail_count
;
77 unsigned int fail_tolerance
;
79 unsigned int buffers_processed
;
80 unsigned int buffers_failed
;
81 unsigned int bytes_processed
;
87 enum pvr2_buffer_state state
;
88 void *ptr
; /* Pointer to storage area */
89 unsigned int max_count
; /* Size of storage area */
90 unsigned int used_count
; /* Amount of valid data in storage area */
91 int status
; /* Transfer result status */
92 struct pvr2_stream
*stream
;
93 struct list_head list_overhead
;
97 static const char *pvr2_buffer_state_decode(enum pvr2_buffer_state st
)
100 case pvr2_buffer_state_none
: return "none";
101 case pvr2_buffer_state_idle
: return "idle";
102 case pvr2_buffer_state_queued
: return "queued";
103 case pvr2_buffer_state_ready
: return "ready";
108 #ifdef SANITY_CHECK_BUFFERS
109 static void pvr2_buffer_describe(struct pvr2_buffer
*bp
, const char *msg
)
111 pvr2_trace(PVR2_TRACE_INFO
,
112 "buffer%s%s %p state=%s id=%d status=%d stream=%p purb=%p sig=0x%x",
116 (bp
? pvr2_buffer_state_decode(bp
->state
) : "(invalid)"),
118 (bp
? bp
->status
: 0),
119 (bp
? bp
->stream
: NULL
),
120 (bp
? bp
->purb
: NULL
),
121 (bp
? bp
->signature
: 0));
123 #endif /* SANITY_CHECK_BUFFERS */
125 static void pvr2_buffer_remove(struct pvr2_buffer
*bp
)
130 struct pvr2_stream
*sp
= bp
->stream
;
132 case pvr2_buffer_state_idle
:
134 bcnt
= &sp
->i_bcount
;
135 ccnt
= bp
->max_count
;
137 case pvr2_buffer_state_queued
:
139 bcnt
= &sp
->q_bcount
;
140 ccnt
= bp
->max_count
;
142 case pvr2_buffer_state_ready
:
144 bcnt
= &sp
->r_bcount
;
145 ccnt
= bp
->used_count
;
150 list_del_init(&bp
->list_overhead
);
153 pvr2_trace(PVR2_TRACE_BUF_FLOW
,
154 "/*---TRACE_FLOW---*/ bufferPool %8s dec cap=%07d cnt=%02d",
155 pvr2_buffer_state_decode(bp
->state
), *bcnt
, *cnt
);
156 bp
->state
= pvr2_buffer_state_none
;
159 static void pvr2_buffer_set_none(struct pvr2_buffer
*bp
)
161 unsigned long irq_flags
;
162 struct pvr2_stream
*sp
;
165 pvr2_trace(PVR2_TRACE_BUF_FLOW
,
166 "/*---TRACE_FLOW---*/ bufferState %p %6s --> %6s",
168 pvr2_buffer_state_decode(bp
->state
),
169 pvr2_buffer_state_decode(pvr2_buffer_state_none
));
170 spin_lock_irqsave(&sp
->list_lock
, irq_flags
);
171 pvr2_buffer_remove(bp
);
172 spin_unlock_irqrestore(&sp
->list_lock
, irq_flags
);
175 static int pvr2_buffer_set_ready(struct pvr2_buffer
*bp
)
178 unsigned long irq_flags
;
179 struct pvr2_stream
*sp
;
182 pvr2_trace(PVR2_TRACE_BUF_FLOW
,
183 "/*---TRACE_FLOW---*/ bufferState %p %6s --> %6s",
185 pvr2_buffer_state_decode(bp
->state
),
186 pvr2_buffer_state_decode(pvr2_buffer_state_ready
));
187 spin_lock_irqsave(&sp
->list_lock
, irq_flags
);
188 fl
= (sp
->r_count
== 0);
189 pvr2_buffer_remove(bp
);
190 list_add_tail(&bp
->list_overhead
, &sp
->ready_list
);
191 bp
->state
= pvr2_buffer_state_ready
;
193 sp
->r_bcount
+= bp
->used_count
;
194 pvr2_trace(PVR2_TRACE_BUF_FLOW
,
195 "/*---TRACE_FLOW---*/ bufferPool %8s inc cap=%07d cnt=%02d",
196 pvr2_buffer_state_decode(bp
->state
),
197 sp
->r_bcount
, sp
->r_count
);
198 spin_unlock_irqrestore(&sp
->list_lock
, irq_flags
);
202 static void pvr2_buffer_set_idle(struct pvr2_buffer
*bp
)
204 unsigned long irq_flags
;
205 struct pvr2_stream
*sp
;
208 pvr2_trace(PVR2_TRACE_BUF_FLOW
,
209 "/*---TRACE_FLOW---*/ bufferState %p %6s --> %6s",
211 pvr2_buffer_state_decode(bp
->state
),
212 pvr2_buffer_state_decode(pvr2_buffer_state_idle
));
213 spin_lock_irqsave(&sp
->list_lock
, irq_flags
);
214 pvr2_buffer_remove(bp
);
215 list_add_tail(&bp
->list_overhead
, &sp
->idle_list
);
216 bp
->state
= pvr2_buffer_state_idle
;
218 sp
->i_bcount
+= bp
->max_count
;
219 pvr2_trace(PVR2_TRACE_BUF_FLOW
,
220 "/*---TRACE_FLOW---*/ bufferPool %8s inc cap=%07d cnt=%02d",
221 pvr2_buffer_state_decode(bp
->state
),
222 sp
->i_bcount
, sp
->i_count
);
223 spin_unlock_irqrestore(&sp
->list_lock
, irq_flags
);
226 static void pvr2_buffer_set_queued(struct pvr2_buffer
*bp
)
228 unsigned long irq_flags
;
229 struct pvr2_stream
*sp
;
232 pvr2_trace(PVR2_TRACE_BUF_FLOW
,
233 "/*---TRACE_FLOW---*/ bufferState %p %6s --> %6s",
235 pvr2_buffer_state_decode(bp
->state
),
236 pvr2_buffer_state_decode(pvr2_buffer_state_queued
));
237 spin_lock_irqsave(&sp
->list_lock
, irq_flags
);
238 pvr2_buffer_remove(bp
);
239 list_add_tail(&bp
->list_overhead
, &sp
->queued_list
);
240 bp
->state
= pvr2_buffer_state_queued
;
242 sp
->q_bcount
+= bp
->max_count
;
243 pvr2_trace(PVR2_TRACE_BUF_FLOW
,
244 "/*---TRACE_FLOW---*/ bufferPool %8s inc cap=%07d cnt=%02d",
245 pvr2_buffer_state_decode(bp
->state
),
246 sp
->q_bcount
, sp
->q_count
);
247 spin_unlock_irqrestore(&sp
->list_lock
, irq_flags
);
250 static void pvr2_buffer_wipe(struct pvr2_buffer
*bp
)
252 if (bp
->state
== pvr2_buffer_state_queued
) {
253 usb_kill_urb(bp
->purb
);
257 static int pvr2_buffer_init(struct pvr2_buffer
*bp
,
258 struct pvr2_stream
*sp
,
261 memset(bp
, 0, sizeof(*bp
));
262 bp
->signature
= BUFFER_SIG
;
264 pvr2_trace(PVR2_TRACE_BUF_POOL
,
265 "/*---TRACE_FLOW---*/ bufferInit %p stream=%p", bp
, sp
);
267 bp
->state
= pvr2_buffer_state_none
;
268 INIT_LIST_HEAD(&bp
->list_overhead
);
269 bp
->purb
= usb_alloc_urb(0, GFP_KERNEL
);
270 if (! bp
->purb
) return -ENOMEM
;
271 #ifdef SANITY_CHECK_BUFFERS
272 pvr2_buffer_describe(bp
, "create");
277 static void pvr2_buffer_done(struct pvr2_buffer
*bp
)
279 #ifdef SANITY_CHECK_BUFFERS
280 pvr2_buffer_describe(bp
, "delete");
282 pvr2_buffer_wipe(bp
);
283 pvr2_buffer_set_none(bp
);
286 usb_free_urb(bp
->purb
);
287 pvr2_trace(PVR2_TRACE_BUF_POOL
, "/*---TRACE_FLOW---*/ bufferDone %p",
291 static int pvr2_stream_buffer_count(struct pvr2_stream
*sp
, unsigned int cnt
)
296 /* Allocate buffers pointer array in multiples of 32 entries */
297 if (cnt
== sp
->buffer_total_count
) return 0;
299 pvr2_trace(PVR2_TRACE_BUF_POOL
,
300 "/*---TRACE_FLOW---*/ poolResize stream=%p cur=%d adj=%+d",
302 sp
->buffer_total_count
,
303 cnt
-sp
->buffer_total_count
);
306 if (cnt
> scnt
) scnt
+= 0x20;
308 if (cnt
> sp
->buffer_total_count
) {
309 if (scnt
> sp
->buffer_slot_count
) {
310 struct pvr2_buffer
**nb
;
312 nb
= kmalloc_array(scnt
, sizeof(*nb
), GFP_KERNEL
);
313 if (!nb
) return -ENOMEM
;
314 if (sp
->buffer_slot_count
) {
315 memcpy(nb
, sp
->buffers
,
316 sp
->buffer_slot_count
* sizeof(*nb
));
320 sp
->buffer_slot_count
= scnt
;
322 while (sp
->buffer_total_count
< cnt
) {
323 struct pvr2_buffer
*bp
;
324 bp
= kmalloc(sizeof(*bp
), GFP_KERNEL
);
325 if (!bp
) return -ENOMEM
;
326 ret
= pvr2_buffer_init(bp
, sp
, sp
->buffer_total_count
);
331 sp
->buffers
[sp
->buffer_total_count
] = bp
;
332 (sp
->buffer_total_count
)++;
333 pvr2_buffer_set_idle(bp
);
336 while (sp
->buffer_total_count
> cnt
) {
337 struct pvr2_buffer
*bp
;
338 bp
= sp
->buffers
[sp
->buffer_total_count
- 1];
340 sp
->buffers
[sp
->buffer_total_count
- 1] = NULL
;
341 (sp
->buffer_total_count
)--;
342 pvr2_buffer_done(bp
);
345 if (scnt
< sp
->buffer_slot_count
) {
346 struct pvr2_buffer
**nb
= NULL
;
348 nb
= kmemdup(sp
->buffers
, scnt
* sizeof(*nb
),
350 if (!nb
) return -ENOMEM
;
354 sp
->buffer_slot_count
= scnt
;
360 static int pvr2_stream_achieve_buffer_count(struct pvr2_stream
*sp
)
362 struct pvr2_buffer
*bp
;
365 if (sp
->buffer_total_count
== sp
->buffer_target_count
) return 0;
367 pvr2_trace(PVR2_TRACE_BUF_POOL
,
368 "/*---TRACE_FLOW---*/ poolCheck stream=%p cur=%d tgt=%d",
369 sp
, sp
->buffer_total_count
, sp
->buffer_target_count
);
371 if (sp
->buffer_total_count
< sp
->buffer_target_count
) {
372 return pvr2_stream_buffer_count(sp
, sp
->buffer_target_count
);
376 while ((sp
->buffer_total_count
- cnt
) > sp
->buffer_target_count
) {
377 bp
= sp
->buffers
[sp
->buffer_total_count
- (cnt
+ 1)];
378 if (bp
->state
!= pvr2_buffer_state_idle
) break;
382 pvr2_stream_buffer_count(sp
, sp
->buffer_total_count
- cnt
);
388 static void pvr2_stream_internal_flush(struct pvr2_stream
*sp
)
390 struct list_head
*lp
;
391 struct pvr2_buffer
*bp1
;
392 while ((lp
= sp
->queued_list
.next
) != &sp
->queued_list
) {
393 bp1
= list_entry(lp
, struct pvr2_buffer
, list_overhead
);
394 pvr2_buffer_wipe(bp1
);
395 /* At this point, we should be guaranteed that no
396 completion callback may happen on this buffer. But it's
397 possible that it might have completed after we noticed
398 it but before we wiped it. So double check its status
400 if (bp1
->state
!= pvr2_buffer_state_queued
) continue;
401 pvr2_buffer_set_idle(bp1
);
403 if (sp
->buffer_total_count
!= sp
->buffer_target_count
) {
404 pvr2_stream_achieve_buffer_count(sp
);
408 static void pvr2_stream_init(struct pvr2_stream
*sp
)
410 spin_lock_init(&sp
->list_lock
);
411 mutex_init(&sp
->mutex
);
412 INIT_LIST_HEAD(&sp
->queued_list
);
413 INIT_LIST_HEAD(&sp
->ready_list
);
414 INIT_LIST_HEAD(&sp
->idle_list
);
417 static void pvr2_stream_done(struct pvr2_stream
*sp
)
419 mutex_lock(&sp
->mutex
); do {
420 pvr2_stream_internal_flush(sp
);
421 pvr2_stream_buffer_count(sp
, 0);
422 } while (0); mutex_unlock(&sp
->mutex
);
425 static void buffer_complete(struct urb
*urb
)
427 struct pvr2_buffer
*bp
= urb
->context
;
428 struct pvr2_stream
*sp
;
429 unsigned long irq_flags
;
434 pvr2_trace(PVR2_TRACE_BUF_FLOW
,
435 "/*---TRACE_FLOW---*/ bufferComplete %p stat=%d cnt=%d",
436 bp
, urb
->status
, urb
->actual_length
);
437 spin_lock_irqsave(&sp
->list_lock
, irq_flags
);
438 if ((!(urb
->status
)) ||
439 (urb
->status
== -ENOENT
) ||
440 (urb
->status
== -ECONNRESET
) ||
441 (urb
->status
== -ESHUTDOWN
)) {
442 (sp
->buffers_processed
)++;
443 sp
->bytes_processed
+= urb
->actual_length
;
444 bp
->used_count
= urb
->actual_length
;
445 if (sp
->fail_count
) {
446 pvr2_trace(PVR2_TRACE_TOLERANCE
,
447 "stream %p transfer ok - fail count reset",
451 } else if (sp
->fail_count
< sp
->fail_tolerance
) {
452 // We can tolerate this error, because we're below the
455 (sp
->buffers_failed
)++;
456 pvr2_trace(PVR2_TRACE_TOLERANCE
,
457 "stream %p ignoring error %d - fail count increased to %u",
458 sp
, urb
->status
, sp
->fail_count
);
460 (sp
->buffers_failed
)++;
461 bp
->status
= urb
->status
;
463 spin_unlock_irqrestore(&sp
->list_lock
, irq_flags
);
464 pvr2_buffer_set_ready(bp
);
465 if (sp
->callback_func
) {
466 sp
->callback_func(sp
->callback_data
);
470 struct pvr2_stream
*pvr2_stream_create(void)
472 struct pvr2_stream
*sp
;
473 sp
= kzalloc(sizeof(*sp
), GFP_KERNEL
);
475 pvr2_trace(PVR2_TRACE_INIT
, "pvr2_stream_create: sp=%p", sp
);
476 pvr2_stream_init(sp
);
480 void pvr2_stream_destroy(struct pvr2_stream
*sp
)
483 pvr2_trace(PVR2_TRACE_INIT
, "pvr2_stream_destroy: sp=%p", sp
);
484 pvr2_stream_done(sp
);
488 void pvr2_stream_setup(struct pvr2_stream
*sp
,
489 struct usb_device
*dev
,
491 unsigned int tolerance
)
493 mutex_lock(&sp
->mutex
); do {
494 pvr2_stream_internal_flush(sp
);
496 sp
->endpoint
= endpoint
;
497 sp
->fail_tolerance
= tolerance
;
498 } while (0); mutex_unlock(&sp
->mutex
);
501 void pvr2_stream_set_callback(struct pvr2_stream
*sp
,
502 pvr2_stream_callback func
,
505 unsigned long irq_flags
;
506 mutex_lock(&sp
->mutex
);
508 spin_lock_irqsave(&sp
->list_lock
, irq_flags
);
509 sp
->callback_data
= data
;
510 sp
->callback_func
= func
;
511 spin_unlock_irqrestore(&sp
->list_lock
, irq_flags
);
513 mutex_unlock(&sp
->mutex
);
516 void pvr2_stream_get_stats(struct pvr2_stream
*sp
,
517 struct pvr2_stream_stats
*stats
,
520 unsigned long irq_flags
;
521 spin_lock_irqsave(&sp
->list_lock
, irq_flags
);
523 stats
->buffers_in_queue
= sp
->q_count
;
524 stats
->buffers_in_idle
= sp
->i_count
;
525 stats
->buffers_in_ready
= sp
->r_count
;
526 stats
->buffers_processed
= sp
->buffers_processed
;
527 stats
->buffers_failed
= sp
->buffers_failed
;
528 stats
->bytes_processed
= sp
->bytes_processed
;
531 sp
->buffers_processed
= 0;
532 sp
->buffers_failed
= 0;
533 sp
->bytes_processed
= 0;
535 spin_unlock_irqrestore(&sp
->list_lock
, irq_flags
);
538 /* Query / set the nominal buffer count */
539 int pvr2_stream_get_buffer_count(struct pvr2_stream
*sp
)
541 return sp
->buffer_target_count
;
544 int pvr2_stream_set_buffer_count(struct pvr2_stream
*sp
, unsigned int cnt
)
547 if (sp
->buffer_target_count
== cnt
) return 0;
548 mutex_lock(&sp
->mutex
);
550 sp
->buffer_target_count
= cnt
;
551 ret
= pvr2_stream_achieve_buffer_count(sp
);
553 mutex_unlock(&sp
->mutex
);
557 struct pvr2_buffer
*pvr2_stream_get_idle_buffer(struct pvr2_stream
*sp
)
559 struct list_head
*lp
= sp
->idle_list
.next
;
560 if (lp
== &sp
->idle_list
) return NULL
;
561 return list_entry(lp
, struct pvr2_buffer
, list_overhead
);
564 struct pvr2_buffer
*pvr2_stream_get_ready_buffer(struct pvr2_stream
*sp
)
566 struct list_head
*lp
= sp
->ready_list
.next
;
567 if (lp
== &sp
->ready_list
) return NULL
;
568 return list_entry(lp
, struct pvr2_buffer
, list_overhead
);
571 struct pvr2_buffer
*pvr2_stream_get_buffer(struct pvr2_stream
*sp
, int id
)
573 if (id
< 0) return NULL
;
574 if (id
>= sp
->buffer_total_count
) return NULL
;
575 return sp
->buffers
[id
];
578 int pvr2_stream_get_ready_count(struct pvr2_stream
*sp
)
583 void pvr2_stream_kill(struct pvr2_stream
*sp
)
585 struct pvr2_buffer
*bp
;
586 mutex_lock(&sp
->mutex
);
588 pvr2_stream_internal_flush(sp
);
589 while ((bp
= pvr2_stream_get_ready_buffer(sp
)) != NULL
) {
590 pvr2_buffer_set_idle(bp
);
592 if (sp
->buffer_total_count
!= sp
->buffer_target_count
) {
593 pvr2_stream_achieve_buffer_count(sp
);
596 mutex_unlock(&sp
->mutex
);
599 int pvr2_buffer_queue(struct pvr2_buffer
*bp
)
607 struct pvr2_stream
*sp
;
608 if (!bp
) return -EINVAL
;
610 mutex_lock(&sp
->mutex
);
612 pvr2_buffer_wipe(bp
);
617 pvr2_buffer_set_queued(bp
);
619 for (idx
= 0; idx
< (bp
->max_count
) / 4; idx
++) {
622 ((unsigned int *)(bp
->ptr
))[idx
] = val
;
625 bp
->status
= -EINPROGRESS
;
626 usb_fill_bulk_urb(bp
->purb
, // struct urb *urb
627 sp
->dev
, // struct usb_device *dev
629 usb_rcvbulkpipe(sp
->dev
, sp
->endpoint
),
630 bp
->ptr
, // void *transfer_buffer
631 bp
->max_count
, // int buffer_length
634 usb_submit_urb(bp
->purb
, GFP_KERNEL
);
636 mutex_unlock(&sp
->mutex
);
640 int pvr2_buffer_set_buffer(struct pvr2_buffer
*bp
, void *ptr
, unsigned int cnt
)
643 unsigned long irq_flags
;
644 struct pvr2_stream
*sp
;
645 if (!bp
) return -EINVAL
;
647 mutex_lock(&sp
->mutex
);
649 spin_lock_irqsave(&sp
->list_lock
, irq_flags
);
650 if (bp
->state
!= pvr2_buffer_state_idle
) {
654 bp
->stream
->i_bcount
-= bp
->max_count
;
656 bp
->stream
->i_bcount
+= bp
->max_count
;
657 pvr2_trace(PVR2_TRACE_BUF_FLOW
,
658 "/*---TRACE_FLOW---*/ bufferPool %8s cap cap=%07d cnt=%02d",
659 pvr2_buffer_state_decode(
660 pvr2_buffer_state_idle
),
661 bp
->stream
->i_bcount
, bp
->stream
->i_count
);
663 spin_unlock_irqrestore(&sp
->list_lock
, irq_flags
);
665 mutex_unlock(&sp
->mutex
);
669 unsigned int pvr2_buffer_get_count(struct pvr2_buffer
*bp
)
671 return bp
->used_count
;
674 int pvr2_buffer_get_status(struct pvr2_buffer
*bp
)
679 int pvr2_buffer_get_id(struct pvr2_buffer
*bp
)