1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
7 #include "pvrusb2-io.h"
8 #include "pvrusb2-debug.h"
9 #include <linux/errno.h>
10 #include <linux/string.h>
11 #include <linux/slab.h>
12 #include <linux/mutex.h>
14 static const char *pvr2_buffer_state_decode(enum pvr2_buffer_state
);
16 #define BUFFER_SIG 0x47653271
18 // #define SANITY_CHECK_BUFFERS
21 #ifdef SANITY_CHECK_BUFFERS
22 #define BUFFER_CHECK(bp) do { \
23 if ((bp)->signature != BUFFER_SIG) { \
24 pvr2_trace(PVR2_TRACE_ERROR_LEGS, \
25 "Buffer %p is bad at %s:%d", \
26 (bp), __FILE__, __LINE__); \
27 pvr2_buffer_describe(bp, "BadSig"); \
32 #define BUFFER_CHECK(bp) do {} while (0)
36 /* Buffers queued for reading */
37 struct list_head queued_list
;
39 unsigned int q_bcount
;
40 /* Buffers with retrieved data */
41 struct list_head ready_list
;
43 unsigned int r_bcount
;
44 /* Buffers available for use */
45 struct list_head idle_list
;
47 unsigned int i_bcount
;
48 /* Pointers to all buffers */
49 struct pvr2_buffer
**buffers
;
50 /* Array size of buffers */
51 unsigned int buffer_slot_count
;
52 /* Total buffers actually in circulation */
53 unsigned int buffer_total_count
;
54 /* Designed number of buffers to be in circulation */
55 unsigned int buffer_target_count
;
56 /* Executed when ready list become non-empty */
57 pvr2_stream_callback callback_func
;
59 /* Context for transfer endpoint */
60 struct usb_device
*dev
;
62 /* Overhead for mutex enforcement */
65 /* Tracking state for tolerating errors */
66 unsigned int fail_count
;
67 unsigned int fail_tolerance
;
69 unsigned int buffers_processed
;
70 unsigned int buffers_failed
;
71 unsigned int bytes_processed
;
77 enum pvr2_buffer_state state
;
78 void *ptr
; /* Pointer to storage area */
79 unsigned int max_count
; /* Size of storage area */
80 unsigned int used_count
; /* Amount of valid data in storage area */
81 int status
; /* Transfer result status */
82 struct pvr2_stream
*stream
;
83 struct list_head list_overhead
;
87 static const char *pvr2_buffer_state_decode(enum pvr2_buffer_state st
)
90 case pvr2_buffer_state_none
: return "none";
91 case pvr2_buffer_state_idle
: return "idle";
92 case pvr2_buffer_state_queued
: return "queued";
93 case pvr2_buffer_state_ready
: return "ready";
98 #ifdef SANITY_CHECK_BUFFERS
99 static void pvr2_buffer_describe(struct pvr2_buffer
*bp
, const char *msg
)
101 pvr2_trace(PVR2_TRACE_INFO
,
102 "buffer%s%s %p state=%s id=%d status=%d stream=%p purb=%p sig=0x%x",
106 (bp
? pvr2_buffer_state_decode(bp
->state
) : "(invalid)"),
108 (bp
? bp
->status
: 0),
109 (bp
? bp
->stream
: NULL
),
110 (bp
? bp
->purb
: NULL
),
111 (bp
? bp
->signature
: 0));
113 #endif /* SANITY_CHECK_BUFFERS */
115 static void pvr2_buffer_remove(struct pvr2_buffer
*bp
)
120 struct pvr2_stream
*sp
= bp
->stream
;
122 case pvr2_buffer_state_idle
:
124 bcnt
= &sp
->i_bcount
;
125 ccnt
= bp
->max_count
;
127 case pvr2_buffer_state_queued
:
129 bcnt
= &sp
->q_bcount
;
130 ccnt
= bp
->max_count
;
132 case pvr2_buffer_state_ready
:
134 bcnt
= &sp
->r_bcount
;
135 ccnt
= bp
->used_count
;
140 list_del_init(&bp
->list_overhead
);
143 pvr2_trace(PVR2_TRACE_BUF_FLOW
,
144 "/*---TRACE_FLOW---*/ bufferPool %8s dec cap=%07d cnt=%02d",
145 pvr2_buffer_state_decode(bp
->state
), *bcnt
, *cnt
);
146 bp
->state
= pvr2_buffer_state_none
;
149 static void pvr2_buffer_set_none(struct pvr2_buffer
*bp
)
151 unsigned long irq_flags
;
152 struct pvr2_stream
*sp
;
155 pvr2_trace(PVR2_TRACE_BUF_FLOW
,
156 "/*---TRACE_FLOW---*/ bufferState %p %6s --> %6s",
158 pvr2_buffer_state_decode(bp
->state
),
159 pvr2_buffer_state_decode(pvr2_buffer_state_none
));
160 spin_lock_irqsave(&sp
->list_lock
, irq_flags
);
161 pvr2_buffer_remove(bp
);
162 spin_unlock_irqrestore(&sp
->list_lock
, irq_flags
);
165 static int pvr2_buffer_set_ready(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_ready
));
177 spin_lock_irqsave(&sp
->list_lock
, irq_flags
);
178 fl
= (sp
->r_count
== 0);
179 pvr2_buffer_remove(bp
);
180 list_add_tail(&bp
->list_overhead
, &sp
->ready_list
);
181 bp
->state
= pvr2_buffer_state_ready
;
183 sp
->r_bcount
+= bp
->used_count
;
184 pvr2_trace(PVR2_TRACE_BUF_FLOW
,
185 "/*---TRACE_FLOW---*/ bufferPool %8s inc cap=%07d cnt=%02d",
186 pvr2_buffer_state_decode(bp
->state
),
187 sp
->r_bcount
, sp
->r_count
);
188 spin_unlock_irqrestore(&sp
->list_lock
, irq_flags
);
192 static void pvr2_buffer_set_idle(struct pvr2_buffer
*bp
)
194 unsigned long irq_flags
;
195 struct pvr2_stream
*sp
;
198 pvr2_trace(PVR2_TRACE_BUF_FLOW
,
199 "/*---TRACE_FLOW---*/ bufferState %p %6s --> %6s",
201 pvr2_buffer_state_decode(bp
->state
),
202 pvr2_buffer_state_decode(pvr2_buffer_state_idle
));
203 spin_lock_irqsave(&sp
->list_lock
, irq_flags
);
204 pvr2_buffer_remove(bp
);
205 list_add_tail(&bp
->list_overhead
, &sp
->idle_list
);
206 bp
->state
= pvr2_buffer_state_idle
;
208 sp
->i_bcount
+= bp
->max_count
;
209 pvr2_trace(PVR2_TRACE_BUF_FLOW
,
210 "/*---TRACE_FLOW---*/ bufferPool %8s inc cap=%07d cnt=%02d",
211 pvr2_buffer_state_decode(bp
->state
),
212 sp
->i_bcount
, sp
->i_count
);
213 spin_unlock_irqrestore(&sp
->list_lock
, irq_flags
);
216 static void pvr2_buffer_set_queued(struct pvr2_buffer
*bp
)
218 unsigned long irq_flags
;
219 struct pvr2_stream
*sp
;
222 pvr2_trace(PVR2_TRACE_BUF_FLOW
,
223 "/*---TRACE_FLOW---*/ bufferState %p %6s --> %6s",
225 pvr2_buffer_state_decode(bp
->state
),
226 pvr2_buffer_state_decode(pvr2_buffer_state_queued
));
227 spin_lock_irqsave(&sp
->list_lock
, irq_flags
);
228 pvr2_buffer_remove(bp
);
229 list_add_tail(&bp
->list_overhead
, &sp
->queued_list
);
230 bp
->state
= pvr2_buffer_state_queued
;
232 sp
->q_bcount
+= bp
->max_count
;
233 pvr2_trace(PVR2_TRACE_BUF_FLOW
,
234 "/*---TRACE_FLOW---*/ bufferPool %8s inc cap=%07d cnt=%02d",
235 pvr2_buffer_state_decode(bp
->state
),
236 sp
->q_bcount
, sp
->q_count
);
237 spin_unlock_irqrestore(&sp
->list_lock
, irq_flags
);
240 static void pvr2_buffer_wipe(struct pvr2_buffer
*bp
)
242 if (bp
->state
== pvr2_buffer_state_queued
) {
243 usb_kill_urb(bp
->purb
);
247 static int pvr2_buffer_init(struct pvr2_buffer
*bp
,
248 struct pvr2_stream
*sp
,
251 memset(bp
, 0, sizeof(*bp
));
252 bp
->signature
= BUFFER_SIG
;
254 pvr2_trace(PVR2_TRACE_BUF_POOL
,
255 "/*---TRACE_FLOW---*/ bufferInit %p stream=%p", bp
, sp
);
257 bp
->state
= pvr2_buffer_state_none
;
258 INIT_LIST_HEAD(&bp
->list_overhead
);
259 bp
->purb
= usb_alloc_urb(0, GFP_KERNEL
);
260 if (! bp
->purb
) return -ENOMEM
;
261 #ifdef SANITY_CHECK_BUFFERS
262 pvr2_buffer_describe(bp
, "create");
267 static void pvr2_buffer_done(struct pvr2_buffer
*bp
)
269 #ifdef SANITY_CHECK_BUFFERS
270 pvr2_buffer_describe(bp
, "delete");
272 pvr2_buffer_wipe(bp
);
273 pvr2_buffer_set_none(bp
);
276 usb_free_urb(bp
->purb
);
277 pvr2_trace(PVR2_TRACE_BUF_POOL
, "/*---TRACE_FLOW---*/ bufferDone %p",
281 static int pvr2_stream_buffer_count(struct pvr2_stream
*sp
, unsigned int cnt
)
286 /* Allocate buffers pointer array in multiples of 32 entries */
287 if (cnt
== sp
->buffer_total_count
) return 0;
289 pvr2_trace(PVR2_TRACE_BUF_POOL
,
290 "/*---TRACE_FLOW---*/ poolResize stream=%p cur=%d adj=%+d",
292 sp
->buffer_total_count
,
293 cnt
-sp
->buffer_total_count
);
296 if (cnt
> scnt
) scnt
+= 0x20;
298 if (cnt
> sp
->buffer_total_count
) {
299 if (scnt
> sp
->buffer_slot_count
) {
300 struct pvr2_buffer
**nb
;
302 nb
= kmalloc_array(scnt
, sizeof(*nb
), GFP_KERNEL
);
303 if (!nb
) return -ENOMEM
;
304 if (sp
->buffer_slot_count
) {
305 memcpy(nb
, sp
->buffers
,
306 sp
->buffer_slot_count
* sizeof(*nb
));
310 sp
->buffer_slot_count
= scnt
;
312 while (sp
->buffer_total_count
< cnt
) {
313 struct pvr2_buffer
*bp
;
314 bp
= kmalloc(sizeof(*bp
), GFP_KERNEL
);
315 if (!bp
) return -ENOMEM
;
316 ret
= pvr2_buffer_init(bp
, sp
, sp
->buffer_total_count
);
321 sp
->buffers
[sp
->buffer_total_count
] = bp
;
322 (sp
->buffer_total_count
)++;
323 pvr2_buffer_set_idle(bp
);
326 while (sp
->buffer_total_count
> cnt
) {
327 struct pvr2_buffer
*bp
;
328 bp
= sp
->buffers
[sp
->buffer_total_count
- 1];
330 sp
->buffers
[sp
->buffer_total_count
- 1] = NULL
;
331 (sp
->buffer_total_count
)--;
332 pvr2_buffer_done(bp
);
335 if (scnt
< sp
->buffer_slot_count
) {
336 struct pvr2_buffer
**nb
= NULL
;
338 nb
= kmemdup(sp
->buffers
, scnt
* sizeof(*nb
),
340 if (!nb
) return -ENOMEM
;
344 sp
->buffer_slot_count
= scnt
;
350 static int pvr2_stream_achieve_buffer_count(struct pvr2_stream
*sp
)
352 struct pvr2_buffer
*bp
;
355 if (sp
->buffer_total_count
== sp
->buffer_target_count
) return 0;
357 pvr2_trace(PVR2_TRACE_BUF_POOL
,
358 "/*---TRACE_FLOW---*/ poolCheck stream=%p cur=%d tgt=%d",
359 sp
, sp
->buffer_total_count
, sp
->buffer_target_count
);
361 if (sp
->buffer_total_count
< sp
->buffer_target_count
) {
362 return pvr2_stream_buffer_count(sp
, sp
->buffer_target_count
);
366 while ((sp
->buffer_total_count
- cnt
) > sp
->buffer_target_count
) {
367 bp
= sp
->buffers
[sp
->buffer_total_count
- (cnt
+ 1)];
368 if (bp
->state
!= pvr2_buffer_state_idle
) break;
372 pvr2_stream_buffer_count(sp
, sp
->buffer_total_count
- cnt
);
378 static void pvr2_stream_internal_flush(struct pvr2_stream
*sp
)
380 struct list_head
*lp
;
381 struct pvr2_buffer
*bp1
;
382 while ((lp
= sp
->queued_list
.next
) != &sp
->queued_list
) {
383 bp1
= list_entry(lp
, struct pvr2_buffer
, list_overhead
);
384 pvr2_buffer_wipe(bp1
);
385 /* At this point, we should be guaranteed that no
386 completion callback may happen on this buffer. But it's
387 possible that it might have completed after we noticed
388 it but before we wiped it. So double check its status
390 if (bp1
->state
!= pvr2_buffer_state_queued
) continue;
391 pvr2_buffer_set_idle(bp1
);
393 if (sp
->buffer_total_count
!= sp
->buffer_target_count
) {
394 pvr2_stream_achieve_buffer_count(sp
);
398 static void pvr2_stream_init(struct pvr2_stream
*sp
)
400 spin_lock_init(&sp
->list_lock
);
401 mutex_init(&sp
->mutex
);
402 INIT_LIST_HEAD(&sp
->queued_list
);
403 INIT_LIST_HEAD(&sp
->ready_list
);
404 INIT_LIST_HEAD(&sp
->idle_list
);
407 static void pvr2_stream_done(struct pvr2_stream
*sp
)
409 mutex_lock(&sp
->mutex
); do {
410 pvr2_stream_internal_flush(sp
);
411 pvr2_stream_buffer_count(sp
, 0);
412 } while (0); mutex_unlock(&sp
->mutex
);
415 static void buffer_complete(struct urb
*urb
)
417 struct pvr2_buffer
*bp
= urb
->context
;
418 struct pvr2_stream
*sp
;
419 unsigned long irq_flags
;
424 pvr2_trace(PVR2_TRACE_BUF_FLOW
,
425 "/*---TRACE_FLOW---*/ bufferComplete %p stat=%d cnt=%d",
426 bp
, urb
->status
, urb
->actual_length
);
427 spin_lock_irqsave(&sp
->list_lock
, irq_flags
);
428 if ((!(urb
->status
)) ||
429 (urb
->status
== -ENOENT
) ||
430 (urb
->status
== -ECONNRESET
) ||
431 (urb
->status
== -ESHUTDOWN
)) {
432 (sp
->buffers_processed
)++;
433 sp
->bytes_processed
+= urb
->actual_length
;
434 bp
->used_count
= urb
->actual_length
;
435 if (sp
->fail_count
) {
436 pvr2_trace(PVR2_TRACE_TOLERANCE
,
437 "stream %p transfer ok - fail count reset",
441 } else if (sp
->fail_count
< sp
->fail_tolerance
) {
442 // We can tolerate this error, because we're below the
445 (sp
->buffers_failed
)++;
446 pvr2_trace(PVR2_TRACE_TOLERANCE
,
447 "stream %p ignoring error %d - fail count increased to %u",
448 sp
, urb
->status
, sp
->fail_count
);
450 (sp
->buffers_failed
)++;
451 bp
->status
= urb
->status
;
453 spin_unlock_irqrestore(&sp
->list_lock
, irq_flags
);
454 pvr2_buffer_set_ready(bp
);
455 if (sp
->callback_func
) {
456 sp
->callback_func(sp
->callback_data
);
460 struct pvr2_stream
*pvr2_stream_create(void)
462 struct pvr2_stream
*sp
;
463 sp
= kzalloc(sizeof(*sp
), GFP_KERNEL
);
465 pvr2_trace(PVR2_TRACE_INIT
, "pvr2_stream_create: sp=%p", sp
);
466 pvr2_stream_init(sp
);
470 void pvr2_stream_destroy(struct pvr2_stream
*sp
)
473 pvr2_trace(PVR2_TRACE_INIT
, "pvr2_stream_destroy: sp=%p", sp
);
474 pvr2_stream_done(sp
);
478 void pvr2_stream_setup(struct pvr2_stream
*sp
,
479 struct usb_device
*dev
,
481 unsigned int tolerance
)
483 mutex_lock(&sp
->mutex
); do {
484 pvr2_stream_internal_flush(sp
);
486 sp
->endpoint
= endpoint
;
487 sp
->fail_tolerance
= tolerance
;
488 } while (0); mutex_unlock(&sp
->mutex
);
491 void pvr2_stream_set_callback(struct pvr2_stream
*sp
,
492 pvr2_stream_callback func
,
495 unsigned long irq_flags
;
496 mutex_lock(&sp
->mutex
);
498 spin_lock_irqsave(&sp
->list_lock
, irq_flags
);
499 sp
->callback_data
= data
;
500 sp
->callback_func
= func
;
501 spin_unlock_irqrestore(&sp
->list_lock
, irq_flags
);
503 mutex_unlock(&sp
->mutex
);
506 void pvr2_stream_get_stats(struct pvr2_stream
*sp
,
507 struct pvr2_stream_stats
*stats
,
510 unsigned long irq_flags
;
511 spin_lock_irqsave(&sp
->list_lock
, irq_flags
);
513 stats
->buffers_in_queue
= sp
->q_count
;
514 stats
->buffers_in_idle
= sp
->i_count
;
515 stats
->buffers_in_ready
= sp
->r_count
;
516 stats
->buffers_processed
= sp
->buffers_processed
;
517 stats
->buffers_failed
= sp
->buffers_failed
;
518 stats
->bytes_processed
= sp
->bytes_processed
;
521 sp
->buffers_processed
= 0;
522 sp
->buffers_failed
= 0;
523 sp
->bytes_processed
= 0;
525 spin_unlock_irqrestore(&sp
->list_lock
, irq_flags
);
528 /* Query / set the nominal buffer count */
529 int pvr2_stream_get_buffer_count(struct pvr2_stream
*sp
)
531 return sp
->buffer_target_count
;
534 int pvr2_stream_set_buffer_count(struct pvr2_stream
*sp
, unsigned int cnt
)
537 if (sp
->buffer_target_count
== cnt
) return 0;
538 mutex_lock(&sp
->mutex
);
540 sp
->buffer_target_count
= cnt
;
541 ret
= pvr2_stream_achieve_buffer_count(sp
);
543 mutex_unlock(&sp
->mutex
);
547 struct pvr2_buffer
*pvr2_stream_get_idle_buffer(struct pvr2_stream
*sp
)
549 struct list_head
*lp
= sp
->idle_list
.next
;
550 if (lp
== &sp
->idle_list
) return NULL
;
551 return list_entry(lp
, struct pvr2_buffer
, list_overhead
);
554 struct pvr2_buffer
*pvr2_stream_get_ready_buffer(struct pvr2_stream
*sp
)
556 struct list_head
*lp
= sp
->ready_list
.next
;
557 if (lp
== &sp
->ready_list
) return NULL
;
558 return list_entry(lp
, struct pvr2_buffer
, list_overhead
);
561 struct pvr2_buffer
*pvr2_stream_get_buffer(struct pvr2_stream
*sp
, int id
)
563 if (id
< 0) return NULL
;
564 if (id
>= sp
->buffer_total_count
) return NULL
;
565 return sp
->buffers
[id
];
568 int pvr2_stream_get_ready_count(struct pvr2_stream
*sp
)
573 void pvr2_stream_kill(struct pvr2_stream
*sp
)
575 struct pvr2_buffer
*bp
;
576 mutex_lock(&sp
->mutex
);
578 pvr2_stream_internal_flush(sp
);
579 while ((bp
= pvr2_stream_get_ready_buffer(sp
)) != NULL
) {
580 pvr2_buffer_set_idle(bp
);
582 if (sp
->buffer_total_count
!= sp
->buffer_target_count
) {
583 pvr2_stream_achieve_buffer_count(sp
);
586 mutex_unlock(&sp
->mutex
);
589 int pvr2_buffer_queue(struct pvr2_buffer
*bp
)
597 struct pvr2_stream
*sp
;
598 if (!bp
) return -EINVAL
;
600 mutex_lock(&sp
->mutex
);
602 pvr2_buffer_wipe(bp
);
607 pvr2_buffer_set_queued(bp
);
609 for (idx
= 0; idx
< (bp
->max_count
) / 4; idx
++) {
612 ((unsigned int *)(bp
->ptr
))[idx
] = val
;
615 bp
->status
= -EINPROGRESS
;
616 usb_fill_bulk_urb(bp
->purb
, // struct urb *urb
617 sp
->dev
, // struct usb_device *dev
619 usb_rcvbulkpipe(sp
->dev
, sp
->endpoint
),
620 bp
->ptr
, // void *transfer_buffer
621 bp
->max_count
, // int buffer_length
624 usb_submit_urb(bp
->purb
, GFP_KERNEL
);
626 mutex_unlock(&sp
->mutex
);
630 int pvr2_buffer_set_buffer(struct pvr2_buffer
*bp
, void *ptr
, unsigned int cnt
)
633 unsigned long irq_flags
;
634 struct pvr2_stream
*sp
;
635 if (!bp
) return -EINVAL
;
637 mutex_lock(&sp
->mutex
);
639 spin_lock_irqsave(&sp
->list_lock
, irq_flags
);
640 if (bp
->state
!= pvr2_buffer_state_idle
) {
644 bp
->stream
->i_bcount
-= bp
->max_count
;
646 bp
->stream
->i_bcount
+= bp
->max_count
;
647 pvr2_trace(PVR2_TRACE_BUF_FLOW
,
648 "/*---TRACE_FLOW---*/ bufferPool %8s cap cap=%07d cnt=%02d",
649 pvr2_buffer_state_decode(
650 pvr2_buffer_state_idle
),
651 bp
->stream
->i_bcount
, bp
->stream
->i_count
);
653 spin_unlock_irqrestore(&sp
->list_lock
, irq_flags
);
655 mutex_unlock(&sp
->mutex
);
659 unsigned int pvr2_buffer_get_count(struct pvr2_buffer
*bp
)
661 return bp
->used_count
;
664 int pvr2_buffer_get_status(struct pvr2_buffer
*bp
)
669 int pvr2_buffer_get_id(struct pvr2_buffer
*bp
)