1*2504ba9fSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
20c0d06caSMauro Carvalho Chehab /*
30c0d06caSMauro Carvalho Chehab *
40c0d06caSMauro Carvalho Chehab * Copyright (C) 2005 Mike Isely <isely@pobox.com>
50c0d06caSMauro Carvalho Chehab */
60c0d06caSMauro Carvalho Chehab
70c0d06caSMauro Carvalho Chehab #include "pvrusb2-io.h"
80c0d06caSMauro Carvalho Chehab #include "pvrusb2-debug.h"
90c0d06caSMauro Carvalho Chehab #include <linux/errno.h>
100c0d06caSMauro Carvalho Chehab #include <linux/string.h>
110c0d06caSMauro Carvalho Chehab #include <linux/slab.h>
120c0d06caSMauro Carvalho Chehab #include <linux/mutex.h>
130c0d06caSMauro Carvalho Chehab
140c0d06caSMauro Carvalho Chehab static const char *pvr2_buffer_state_decode(enum pvr2_buffer_state);
150c0d06caSMauro Carvalho Chehab
160c0d06caSMauro Carvalho Chehab #define BUFFER_SIG 0x47653271
170c0d06caSMauro Carvalho Chehab
180c0d06caSMauro Carvalho Chehab // #define SANITY_CHECK_BUFFERS
190c0d06caSMauro Carvalho Chehab
200c0d06caSMauro Carvalho Chehab
210c0d06caSMauro Carvalho Chehab #ifdef SANITY_CHECK_BUFFERS
220c0d06caSMauro Carvalho Chehab #define BUFFER_CHECK(bp) do { \
230c0d06caSMauro Carvalho Chehab if ((bp)->signature != BUFFER_SIG) { \
240c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_ERROR_LEGS, \
250c0d06caSMauro Carvalho Chehab "Buffer %p is bad at %s:%d", \
260c0d06caSMauro Carvalho Chehab (bp), __FILE__, __LINE__); \
270c0d06caSMauro Carvalho Chehab pvr2_buffer_describe(bp, "BadSig"); \
280c0d06caSMauro Carvalho Chehab BUG(); \
290c0d06caSMauro Carvalho Chehab } \
300c0d06caSMauro Carvalho Chehab } while (0)
310c0d06caSMauro Carvalho Chehab #else
320c0d06caSMauro Carvalho Chehab #define BUFFER_CHECK(bp) do {} while (0)
330c0d06caSMauro Carvalho Chehab #endif
340c0d06caSMauro Carvalho Chehab
350c0d06caSMauro Carvalho Chehab struct pvr2_stream {
360c0d06caSMauro Carvalho Chehab /* Buffers queued for reading */
370c0d06caSMauro Carvalho Chehab struct list_head queued_list;
380c0d06caSMauro Carvalho Chehab unsigned int q_count;
390c0d06caSMauro Carvalho Chehab unsigned int q_bcount;
400c0d06caSMauro Carvalho Chehab /* Buffers with retrieved data */
410c0d06caSMauro Carvalho Chehab struct list_head ready_list;
420c0d06caSMauro Carvalho Chehab unsigned int r_count;
430c0d06caSMauro Carvalho Chehab unsigned int r_bcount;
440c0d06caSMauro Carvalho Chehab /* Buffers available for use */
450c0d06caSMauro Carvalho Chehab struct list_head idle_list;
460c0d06caSMauro Carvalho Chehab unsigned int i_count;
470c0d06caSMauro Carvalho Chehab unsigned int i_bcount;
480c0d06caSMauro Carvalho Chehab /* Pointers to all buffers */
490c0d06caSMauro Carvalho Chehab struct pvr2_buffer **buffers;
500c0d06caSMauro Carvalho Chehab /* Array size of buffers */
510c0d06caSMauro Carvalho Chehab unsigned int buffer_slot_count;
520c0d06caSMauro Carvalho Chehab /* Total buffers actually in circulation */
530c0d06caSMauro Carvalho Chehab unsigned int buffer_total_count;
540c0d06caSMauro Carvalho Chehab /* Designed number of buffers to be in circulation */
550c0d06caSMauro Carvalho Chehab unsigned int buffer_target_count;
560c0d06caSMauro Carvalho Chehab /* Executed when ready list become non-empty */
570c0d06caSMauro Carvalho Chehab pvr2_stream_callback callback_func;
580c0d06caSMauro Carvalho Chehab void *callback_data;
590c0d06caSMauro Carvalho Chehab /* Context for transfer endpoint */
600c0d06caSMauro Carvalho Chehab struct usb_device *dev;
610c0d06caSMauro Carvalho Chehab int endpoint;
620c0d06caSMauro Carvalho Chehab /* Overhead for mutex enforcement */
630c0d06caSMauro Carvalho Chehab spinlock_t list_lock;
640c0d06caSMauro Carvalho Chehab struct mutex mutex;
650c0d06caSMauro Carvalho Chehab /* Tracking state for tolerating errors */
660c0d06caSMauro Carvalho Chehab unsigned int fail_count;
670c0d06caSMauro Carvalho Chehab unsigned int fail_tolerance;
680c0d06caSMauro Carvalho Chehab
690c0d06caSMauro Carvalho Chehab unsigned int buffers_processed;
700c0d06caSMauro Carvalho Chehab unsigned int buffers_failed;
710c0d06caSMauro Carvalho Chehab unsigned int bytes_processed;
720c0d06caSMauro Carvalho Chehab };
730c0d06caSMauro Carvalho Chehab
740c0d06caSMauro Carvalho Chehab struct pvr2_buffer {
750c0d06caSMauro Carvalho Chehab int id;
760c0d06caSMauro Carvalho Chehab int signature;
770c0d06caSMauro Carvalho Chehab enum pvr2_buffer_state state;
780c0d06caSMauro Carvalho Chehab void *ptr; /* Pointer to storage area */
790c0d06caSMauro Carvalho Chehab unsigned int max_count; /* Size of storage area */
800c0d06caSMauro Carvalho Chehab unsigned int used_count; /* Amount of valid data in storage area */
810c0d06caSMauro Carvalho Chehab int status; /* Transfer result status */
820c0d06caSMauro Carvalho Chehab struct pvr2_stream *stream;
830c0d06caSMauro Carvalho Chehab struct list_head list_overhead;
840c0d06caSMauro Carvalho Chehab struct urb *purb;
850c0d06caSMauro Carvalho Chehab };
860c0d06caSMauro Carvalho Chehab
pvr2_buffer_state_decode(enum pvr2_buffer_state st)870c0d06caSMauro Carvalho Chehab static const char *pvr2_buffer_state_decode(enum pvr2_buffer_state st)
880c0d06caSMauro Carvalho Chehab {
890c0d06caSMauro Carvalho Chehab switch (st) {
900c0d06caSMauro Carvalho Chehab case pvr2_buffer_state_none: return "none";
910c0d06caSMauro Carvalho Chehab case pvr2_buffer_state_idle: return "idle";
920c0d06caSMauro Carvalho Chehab case pvr2_buffer_state_queued: return "queued";
930c0d06caSMauro Carvalho Chehab case pvr2_buffer_state_ready: return "ready";
940c0d06caSMauro Carvalho Chehab }
950c0d06caSMauro Carvalho Chehab return "unknown";
960c0d06caSMauro Carvalho Chehab }
970c0d06caSMauro Carvalho Chehab
980c0d06caSMauro Carvalho Chehab #ifdef SANITY_CHECK_BUFFERS
pvr2_buffer_describe(struct pvr2_buffer * bp,const char * msg)990c0d06caSMauro Carvalho Chehab static void pvr2_buffer_describe(struct pvr2_buffer *bp, const char *msg)
1000c0d06caSMauro Carvalho Chehab {
1010c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_INFO,
10296292c89SMauro Carvalho Chehab "buffer%s%s %p state=%s id=%d status=%d stream=%p purb=%p sig=0x%x",
1030c0d06caSMauro Carvalho Chehab (msg ? " " : ""),
1040c0d06caSMauro Carvalho Chehab (msg ? msg : ""),
1050c0d06caSMauro Carvalho Chehab bp,
1060c0d06caSMauro Carvalho Chehab (bp ? pvr2_buffer_state_decode(bp->state) : "(invalid)"),
1070c0d06caSMauro Carvalho Chehab (bp ? bp->id : 0),
1080c0d06caSMauro Carvalho Chehab (bp ? bp->status : 0),
1090c0d06caSMauro Carvalho Chehab (bp ? bp->stream : NULL),
1100c0d06caSMauro Carvalho Chehab (bp ? bp->purb : NULL),
1110c0d06caSMauro Carvalho Chehab (bp ? bp->signature : 0));
1120c0d06caSMauro Carvalho Chehab }
1130c0d06caSMauro Carvalho Chehab #endif /* SANITY_CHECK_BUFFERS */
1140c0d06caSMauro Carvalho Chehab
pvr2_buffer_remove(struct pvr2_buffer * bp)1150c0d06caSMauro Carvalho Chehab static void pvr2_buffer_remove(struct pvr2_buffer *bp)
1160c0d06caSMauro Carvalho Chehab {
1170c0d06caSMauro Carvalho Chehab unsigned int *cnt;
1180c0d06caSMauro Carvalho Chehab unsigned int *bcnt;
1190c0d06caSMauro Carvalho Chehab unsigned int ccnt;
1200c0d06caSMauro Carvalho Chehab struct pvr2_stream *sp = bp->stream;
1210c0d06caSMauro Carvalho Chehab switch (bp->state) {
1220c0d06caSMauro Carvalho Chehab case pvr2_buffer_state_idle:
1230c0d06caSMauro Carvalho Chehab cnt = &sp->i_count;
1240c0d06caSMauro Carvalho Chehab bcnt = &sp->i_bcount;
1250c0d06caSMauro Carvalho Chehab ccnt = bp->max_count;
1260c0d06caSMauro Carvalho Chehab break;
1270c0d06caSMauro Carvalho Chehab case pvr2_buffer_state_queued:
1280c0d06caSMauro Carvalho Chehab cnt = &sp->q_count;
1290c0d06caSMauro Carvalho Chehab bcnt = &sp->q_bcount;
1300c0d06caSMauro Carvalho Chehab ccnt = bp->max_count;
1310c0d06caSMauro Carvalho Chehab break;
1320c0d06caSMauro Carvalho Chehab case pvr2_buffer_state_ready:
1330c0d06caSMauro Carvalho Chehab cnt = &sp->r_count;
1340c0d06caSMauro Carvalho Chehab bcnt = &sp->r_bcount;
1350c0d06caSMauro Carvalho Chehab ccnt = bp->used_count;
1360c0d06caSMauro Carvalho Chehab break;
1370c0d06caSMauro Carvalho Chehab default:
1380c0d06caSMauro Carvalho Chehab return;
1390c0d06caSMauro Carvalho Chehab }
1400c0d06caSMauro Carvalho Chehab list_del_init(&bp->list_overhead);
1410c0d06caSMauro Carvalho Chehab (*cnt)--;
1420c0d06caSMauro Carvalho Chehab (*bcnt) -= ccnt;
1430c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_BUF_FLOW,
14496292c89SMauro Carvalho Chehab "/*---TRACE_FLOW---*/ bufferPool %8s dec cap=%07d cnt=%02d",
1450c0d06caSMauro Carvalho Chehab pvr2_buffer_state_decode(bp->state), *bcnt, *cnt);
1460c0d06caSMauro Carvalho Chehab bp->state = pvr2_buffer_state_none;
1470c0d06caSMauro Carvalho Chehab }
1480c0d06caSMauro Carvalho Chehab
pvr2_buffer_set_none(struct pvr2_buffer * bp)1490c0d06caSMauro Carvalho Chehab static void pvr2_buffer_set_none(struct pvr2_buffer *bp)
1500c0d06caSMauro Carvalho Chehab {
1510c0d06caSMauro Carvalho Chehab unsigned long irq_flags;
1520c0d06caSMauro Carvalho Chehab struct pvr2_stream *sp;
1530c0d06caSMauro Carvalho Chehab BUFFER_CHECK(bp);
1540c0d06caSMauro Carvalho Chehab sp = bp->stream;
1550c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_BUF_FLOW,
1560c0d06caSMauro Carvalho Chehab "/*---TRACE_FLOW---*/ bufferState %p %6s --> %6s",
1570c0d06caSMauro Carvalho Chehab bp,
1580c0d06caSMauro Carvalho Chehab pvr2_buffer_state_decode(bp->state),
1590c0d06caSMauro Carvalho Chehab pvr2_buffer_state_decode(pvr2_buffer_state_none));
1600c0d06caSMauro Carvalho Chehab spin_lock_irqsave(&sp->list_lock, irq_flags);
1610c0d06caSMauro Carvalho Chehab pvr2_buffer_remove(bp);
1620c0d06caSMauro Carvalho Chehab spin_unlock_irqrestore(&sp->list_lock, irq_flags);
1630c0d06caSMauro Carvalho Chehab }
1640c0d06caSMauro Carvalho Chehab
pvr2_buffer_set_ready(struct pvr2_buffer * bp)1650c0d06caSMauro Carvalho Chehab static int pvr2_buffer_set_ready(struct pvr2_buffer *bp)
1660c0d06caSMauro Carvalho Chehab {
1670c0d06caSMauro Carvalho Chehab int fl;
1680c0d06caSMauro Carvalho Chehab unsigned long irq_flags;
1690c0d06caSMauro Carvalho Chehab struct pvr2_stream *sp;
1700c0d06caSMauro Carvalho Chehab BUFFER_CHECK(bp);
1710c0d06caSMauro Carvalho Chehab sp = bp->stream;
1720c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_BUF_FLOW,
1730c0d06caSMauro Carvalho Chehab "/*---TRACE_FLOW---*/ bufferState %p %6s --> %6s",
1740c0d06caSMauro Carvalho Chehab bp,
1750c0d06caSMauro Carvalho Chehab pvr2_buffer_state_decode(bp->state),
1760c0d06caSMauro Carvalho Chehab pvr2_buffer_state_decode(pvr2_buffer_state_ready));
1770c0d06caSMauro Carvalho Chehab spin_lock_irqsave(&sp->list_lock, irq_flags);
1780c0d06caSMauro Carvalho Chehab fl = (sp->r_count == 0);
1790c0d06caSMauro Carvalho Chehab pvr2_buffer_remove(bp);
1800c0d06caSMauro Carvalho Chehab list_add_tail(&bp->list_overhead, &sp->ready_list);
1810c0d06caSMauro Carvalho Chehab bp->state = pvr2_buffer_state_ready;
1820c0d06caSMauro Carvalho Chehab (sp->r_count)++;
1830c0d06caSMauro Carvalho Chehab sp->r_bcount += bp->used_count;
1840c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_BUF_FLOW,
18596292c89SMauro Carvalho Chehab "/*---TRACE_FLOW---*/ bufferPool %8s inc cap=%07d cnt=%02d",
1860c0d06caSMauro Carvalho Chehab pvr2_buffer_state_decode(bp->state),
1870c0d06caSMauro Carvalho Chehab sp->r_bcount, sp->r_count);
1880c0d06caSMauro Carvalho Chehab spin_unlock_irqrestore(&sp->list_lock, irq_flags);
1890c0d06caSMauro Carvalho Chehab return fl;
1900c0d06caSMauro Carvalho Chehab }
1910c0d06caSMauro Carvalho Chehab
pvr2_buffer_set_idle(struct pvr2_buffer * bp)1920c0d06caSMauro Carvalho Chehab static void pvr2_buffer_set_idle(struct pvr2_buffer *bp)
1930c0d06caSMauro Carvalho Chehab {
1940c0d06caSMauro Carvalho Chehab unsigned long irq_flags;
1950c0d06caSMauro Carvalho Chehab struct pvr2_stream *sp;
1960c0d06caSMauro Carvalho Chehab BUFFER_CHECK(bp);
1970c0d06caSMauro Carvalho Chehab sp = bp->stream;
1980c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_BUF_FLOW,
1990c0d06caSMauro Carvalho Chehab "/*---TRACE_FLOW---*/ bufferState %p %6s --> %6s",
2000c0d06caSMauro Carvalho Chehab bp,
2010c0d06caSMauro Carvalho Chehab pvr2_buffer_state_decode(bp->state),
2020c0d06caSMauro Carvalho Chehab pvr2_buffer_state_decode(pvr2_buffer_state_idle));
2030c0d06caSMauro Carvalho Chehab spin_lock_irqsave(&sp->list_lock, irq_flags);
2040c0d06caSMauro Carvalho Chehab pvr2_buffer_remove(bp);
2050c0d06caSMauro Carvalho Chehab list_add_tail(&bp->list_overhead, &sp->idle_list);
2060c0d06caSMauro Carvalho Chehab bp->state = pvr2_buffer_state_idle;
2070c0d06caSMauro Carvalho Chehab (sp->i_count)++;
2080c0d06caSMauro Carvalho Chehab sp->i_bcount += bp->max_count;
2090c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_BUF_FLOW,
21096292c89SMauro Carvalho Chehab "/*---TRACE_FLOW---*/ bufferPool %8s inc cap=%07d cnt=%02d",
2110c0d06caSMauro Carvalho Chehab pvr2_buffer_state_decode(bp->state),
2120c0d06caSMauro Carvalho Chehab sp->i_bcount, sp->i_count);
2130c0d06caSMauro Carvalho Chehab spin_unlock_irqrestore(&sp->list_lock, irq_flags);
2140c0d06caSMauro Carvalho Chehab }
2150c0d06caSMauro Carvalho Chehab
pvr2_buffer_set_queued(struct pvr2_buffer * bp)2160c0d06caSMauro Carvalho Chehab static void pvr2_buffer_set_queued(struct pvr2_buffer *bp)
2170c0d06caSMauro Carvalho Chehab {
2180c0d06caSMauro Carvalho Chehab unsigned long irq_flags;
2190c0d06caSMauro Carvalho Chehab struct pvr2_stream *sp;
2200c0d06caSMauro Carvalho Chehab BUFFER_CHECK(bp);
2210c0d06caSMauro Carvalho Chehab sp = bp->stream;
2220c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_BUF_FLOW,
2230c0d06caSMauro Carvalho Chehab "/*---TRACE_FLOW---*/ bufferState %p %6s --> %6s",
2240c0d06caSMauro Carvalho Chehab bp,
2250c0d06caSMauro Carvalho Chehab pvr2_buffer_state_decode(bp->state),
2260c0d06caSMauro Carvalho Chehab pvr2_buffer_state_decode(pvr2_buffer_state_queued));
2270c0d06caSMauro Carvalho Chehab spin_lock_irqsave(&sp->list_lock, irq_flags);
2280c0d06caSMauro Carvalho Chehab pvr2_buffer_remove(bp);
2290c0d06caSMauro Carvalho Chehab list_add_tail(&bp->list_overhead, &sp->queued_list);
2300c0d06caSMauro Carvalho Chehab bp->state = pvr2_buffer_state_queued;
2310c0d06caSMauro Carvalho Chehab (sp->q_count)++;
2320c0d06caSMauro Carvalho Chehab sp->q_bcount += bp->max_count;
2330c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_BUF_FLOW,
23496292c89SMauro Carvalho Chehab "/*---TRACE_FLOW---*/ bufferPool %8s inc cap=%07d cnt=%02d",
2350c0d06caSMauro Carvalho Chehab pvr2_buffer_state_decode(bp->state),
2360c0d06caSMauro Carvalho Chehab sp->q_bcount, sp->q_count);
2370c0d06caSMauro Carvalho Chehab spin_unlock_irqrestore(&sp->list_lock, irq_flags);
2380c0d06caSMauro Carvalho Chehab }
2390c0d06caSMauro Carvalho Chehab
pvr2_buffer_wipe(struct pvr2_buffer * bp)2400c0d06caSMauro Carvalho Chehab static void pvr2_buffer_wipe(struct pvr2_buffer *bp)
2410c0d06caSMauro Carvalho Chehab {
2420c0d06caSMauro Carvalho Chehab if (bp->state == pvr2_buffer_state_queued) {
2430c0d06caSMauro Carvalho Chehab usb_kill_urb(bp->purb);
2440c0d06caSMauro Carvalho Chehab }
2450c0d06caSMauro Carvalho Chehab }
2460c0d06caSMauro Carvalho Chehab
pvr2_buffer_init(struct pvr2_buffer * bp,struct pvr2_stream * sp,unsigned int id)2470c0d06caSMauro Carvalho Chehab static int pvr2_buffer_init(struct pvr2_buffer *bp,
2480c0d06caSMauro Carvalho Chehab struct pvr2_stream *sp,
2490c0d06caSMauro Carvalho Chehab unsigned int id)
2500c0d06caSMauro Carvalho Chehab {
2510c0d06caSMauro Carvalho Chehab memset(bp, 0, sizeof(*bp));
2520c0d06caSMauro Carvalho Chehab bp->signature = BUFFER_SIG;
2530c0d06caSMauro Carvalho Chehab bp->id = id;
2540c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_BUF_POOL,
2550c0d06caSMauro Carvalho Chehab "/*---TRACE_FLOW---*/ bufferInit %p stream=%p", bp, sp);
2560c0d06caSMauro Carvalho Chehab bp->stream = sp;
2570c0d06caSMauro Carvalho Chehab bp->state = pvr2_buffer_state_none;
2580c0d06caSMauro Carvalho Chehab INIT_LIST_HEAD(&bp->list_overhead);
2590c0d06caSMauro Carvalho Chehab bp->purb = usb_alloc_urb(0, GFP_KERNEL);
2600c0d06caSMauro Carvalho Chehab if (! bp->purb) return -ENOMEM;
2610c0d06caSMauro Carvalho Chehab #ifdef SANITY_CHECK_BUFFERS
2620c0d06caSMauro Carvalho Chehab pvr2_buffer_describe(bp, "create");
2630c0d06caSMauro Carvalho Chehab #endif
2640c0d06caSMauro Carvalho Chehab return 0;
2650c0d06caSMauro Carvalho Chehab }
2660c0d06caSMauro Carvalho Chehab
pvr2_buffer_done(struct pvr2_buffer * bp)2670c0d06caSMauro Carvalho Chehab static void pvr2_buffer_done(struct pvr2_buffer *bp)
2680c0d06caSMauro Carvalho Chehab {
2690c0d06caSMauro Carvalho Chehab #ifdef SANITY_CHECK_BUFFERS
2700c0d06caSMauro Carvalho Chehab pvr2_buffer_describe(bp, "delete");
2710c0d06caSMauro Carvalho Chehab #endif
2720c0d06caSMauro Carvalho Chehab pvr2_buffer_wipe(bp);
2730c0d06caSMauro Carvalho Chehab pvr2_buffer_set_none(bp);
2740c0d06caSMauro Carvalho Chehab bp->signature = 0;
2750c0d06caSMauro Carvalho Chehab bp->stream = NULL;
2760c0d06caSMauro Carvalho Chehab usb_free_urb(bp->purb);
27796292c89SMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_BUF_POOL, "/*---TRACE_FLOW---*/ bufferDone %p",
27896292c89SMauro Carvalho Chehab bp);
2790c0d06caSMauro Carvalho Chehab }
2800c0d06caSMauro Carvalho Chehab
pvr2_stream_buffer_count(struct pvr2_stream * sp,unsigned int cnt)2810c0d06caSMauro Carvalho Chehab static int pvr2_stream_buffer_count(struct pvr2_stream *sp, unsigned int cnt)
2820c0d06caSMauro Carvalho Chehab {
2830c0d06caSMauro Carvalho Chehab int ret;
2840c0d06caSMauro Carvalho Chehab unsigned int scnt;
2850c0d06caSMauro Carvalho Chehab
2860c0d06caSMauro Carvalho Chehab /* Allocate buffers pointer array in multiples of 32 entries */
2870c0d06caSMauro Carvalho Chehab if (cnt == sp->buffer_total_count) return 0;
2880c0d06caSMauro Carvalho Chehab
2890c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_BUF_POOL,
29096292c89SMauro Carvalho Chehab "/*---TRACE_FLOW---*/ poolResize stream=%p cur=%d adj=%+d",
2910c0d06caSMauro Carvalho Chehab sp,
2920c0d06caSMauro Carvalho Chehab sp->buffer_total_count,
2930c0d06caSMauro Carvalho Chehab cnt-sp->buffer_total_count);
2940c0d06caSMauro Carvalho Chehab
2950c0d06caSMauro Carvalho Chehab scnt = cnt & ~0x1f;
2960c0d06caSMauro Carvalho Chehab if (cnt > scnt) scnt += 0x20;
2970c0d06caSMauro Carvalho Chehab
2980c0d06caSMauro Carvalho Chehab if (cnt > sp->buffer_total_count) {
2990c0d06caSMauro Carvalho Chehab if (scnt > sp->buffer_slot_count) {
3000c0d06caSMauro Carvalho Chehab struct pvr2_buffer **nb;
30116eddbe3SMarkus Elfring
30216eddbe3SMarkus Elfring nb = kmalloc_array(scnt, sizeof(*nb), GFP_KERNEL);
3030c0d06caSMauro Carvalho Chehab if (!nb) return -ENOMEM;
3040c0d06caSMauro Carvalho Chehab if (sp->buffer_slot_count) {
3050c0d06caSMauro Carvalho Chehab memcpy(nb, sp->buffers,
3060c0d06caSMauro Carvalho Chehab sp->buffer_slot_count * sizeof(*nb));
3070c0d06caSMauro Carvalho Chehab kfree(sp->buffers);
3080c0d06caSMauro Carvalho Chehab }
3090c0d06caSMauro Carvalho Chehab sp->buffers = nb;
3100c0d06caSMauro Carvalho Chehab sp->buffer_slot_count = scnt;
3110c0d06caSMauro Carvalho Chehab }
3120c0d06caSMauro Carvalho Chehab while (sp->buffer_total_count < cnt) {
3130c0d06caSMauro Carvalho Chehab struct pvr2_buffer *bp;
3140c0d06caSMauro Carvalho Chehab bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3150c0d06caSMauro Carvalho Chehab if (!bp) return -ENOMEM;
3160c0d06caSMauro Carvalho Chehab ret = pvr2_buffer_init(bp, sp, sp->buffer_total_count);
3170c0d06caSMauro Carvalho Chehab if (ret) {
3180c0d06caSMauro Carvalho Chehab kfree(bp);
3190c0d06caSMauro Carvalho Chehab return -ENOMEM;
3200c0d06caSMauro Carvalho Chehab }
3210c0d06caSMauro Carvalho Chehab sp->buffers[sp->buffer_total_count] = bp;
3220c0d06caSMauro Carvalho Chehab (sp->buffer_total_count)++;
3230c0d06caSMauro Carvalho Chehab pvr2_buffer_set_idle(bp);
3240c0d06caSMauro Carvalho Chehab }
3250c0d06caSMauro Carvalho Chehab } else {
3260c0d06caSMauro Carvalho Chehab while (sp->buffer_total_count > cnt) {
3270c0d06caSMauro Carvalho Chehab struct pvr2_buffer *bp;
3280c0d06caSMauro Carvalho Chehab bp = sp->buffers[sp->buffer_total_count - 1];
3290c0d06caSMauro Carvalho Chehab /* Paranoia */
3300c0d06caSMauro Carvalho Chehab sp->buffers[sp->buffer_total_count - 1] = NULL;
3310c0d06caSMauro Carvalho Chehab (sp->buffer_total_count)--;
3320c0d06caSMauro Carvalho Chehab pvr2_buffer_done(bp);
3330c0d06caSMauro Carvalho Chehab kfree(bp);
3340c0d06caSMauro Carvalho Chehab }
3350c0d06caSMauro Carvalho Chehab if (scnt < sp->buffer_slot_count) {
3360c0d06caSMauro Carvalho Chehab struct pvr2_buffer **nb = NULL;
3370c0d06caSMauro Carvalho Chehab if (scnt) {
33807342664SThomas Meyer nb = kmemdup(sp->buffers, scnt * sizeof(*nb),
33907342664SThomas Meyer GFP_KERNEL);
3400c0d06caSMauro Carvalho Chehab if (!nb) return -ENOMEM;
3410c0d06caSMauro Carvalho Chehab }
3420c0d06caSMauro Carvalho Chehab kfree(sp->buffers);
3430c0d06caSMauro Carvalho Chehab sp->buffers = nb;
3440c0d06caSMauro Carvalho Chehab sp->buffer_slot_count = scnt;
3450c0d06caSMauro Carvalho Chehab }
3460c0d06caSMauro Carvalho Chehab }
3470c0d06caSMauro Carvalho Chehab return 0;
3480c0d06caSMauro Carvalho Chehab }
3490c0d06caSMauro Carvalho Chehab
pvr2_stream_achieve_buffer_count(struct pvr2_stream * sp)3500c0d06caSMauro Carvalho Chehab static int pvr2_stream_achieve_buffer_count(struct pvr2_stream *sp)
3510c0d06caSMauro Carvalho Chehab {
3520c0d06caSMauro Carvalho Chehab struct pvr2_buffer *bp;
3530c0d06caSMauro Carvalho Chehab unsigned int cnt;
3540c0d06caSMauro Carvalho Chehab
3550c0d06caSMauro Carvalho Chehab if (sp->buffer_total_count == sp->buffer_target_count) return 0;
3560c0d06caSMauro Carvalho Chehab
3570c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_BUF_POOL,
35896292c89SMauro Carvalho Chehab "/*---TRACE_FLOW---*/ poolCheck stream=%p cur=%d tgt=%d",
3590c0d06caSMauro Carvalho Chehab sp, sp->buffer_total_count, sp->buffer_target_count);
3600c0d06caSMauro Carvalho Chehab
3610c0d06caSMauro Carvalho Chehab if (sp->buffer_total_count < sp->buffer_target_count) {
3620c0d06caSMauro Carvalho Chehab return pvr2_stream_buffer_count(sp, sp->buffer_target_count);
3630c0d06caSMauro Carvalho Chehab }
3640c0d06caSMauro Carvalho Chehab
3650c0d06caSMauro Carvalho Chehab cnt = 0;
3660c0d06caSMauro Carvalho Chehab while ((sp->buffer_total_count - cnt) > sp->buffer_target_count) {
3670c0d06caSMauro Carvalho Chehab bp = sp->buffers[sp->buffer_total_count - (cnt + 1)];
3680c0d06caSMauro Carvalho Chehab if (bp->state != pvr2_buffer_state_idle) break;
3690c0d06caSMauro Carvalho Chehab cnt++;
3700c0d06caSMauro Carvalho Chehab }
3710c0d06caSMauro Carvalho Chehab if (cnt) {
3720c0d06caSMauro Carvalho Chehab pvr2_stream_buffer_count(sp, sp->buffer_total_count - cnt);
3730c0d06caSMauro Carvalho Chehab }
3740c0d06caSMauro Carvalho Chehab
3750c0d06caSMauro Carvalho Chehab return 0;
3760c0d06caSMauro Carvalho Chehab }
3770c0d06caSMauro Carvalho Chehab
pvr2_stream_internal_flush(struct pvr2_stream * sp)3780c0d06caSMauro Carvalho Chehab static void pvr2_stream_internal_flush(struct pvr2_stream *sp)
3790c0d06caSMauro Carvalho Chehab {
3800c0d06caSMauro Carvalho Chehab struct list_head *lp;
3810c0d06caSMauro Carvalho Chehab struct pvr2_buffer *bp1;
3820c0d06caSMauro Carvalho Chehab while ((lp = sp->queued_list.next) != &sp->queued_list) {
3830c0d06caSMauro Carvalho Chehab bp1 = list_entry(lp, struct pvr2_buffer, list_overhead);
3840c0d06caSMauro Carvalho Chehab pvr2_buffer_wipe(bp1);
3850c0d06caSMauro Carvalho Chehab /* At this point, we should be guaranteed that no
3860c0d06caSMauro Carvalho Chehab completion callback may happen on this buffer. But it's
3870c0d06caSMauro Carvalho Chehab possible that it might have completed after we noticed
3880c0d06caSMauro Carvalho Chehab it but before we wiped it. So double check its status
3890c0d06caSMauro Carvalho Chehab here first. */
3900c0d06caSMauro Carvalho Chehab if (bp1->state != pvr2_buffer_state_queued) continue;
3910c0d06caSMauro Carvalho Chehab pvr2_buffer_set_idle(bp1);
3920c0d06caSMauro Carvalho Chehab }
3930c0d06caSMauro Carvalho Chehab if (sp->buffer_total_count != sp->buffer_target_count) {
3940c0d06caSMauro Carvalho Chehab pvr2_stream_achieve_buffer_count(sp);
3950c0d06caSMauro Carvalho Chehab }
3960c0d06caSMauro Carvalho Chehab }
3970c0d06caSMauro Carvalho Chehab
pvr2_stream_init(struct pvr2_stream * sp)3980c0d06caSMauro Carvalho Chehab static void pvr2_stream_init(struct pvr2_stream *sp)
3990c0d06caSMauro Carvalho Chehab {
4000c0d06caSMauro Carvalho Chehab spin_lock_init(&sp->list_lock);
4010c0d06caSMauro Carvalho Chehab mutex_init(&sp->mutex);
4020c0d06caSMauro Carvalho Chehab INIT_LIST_HEAD(&sp->queued_list);
4030c0d06caSMauro Carvalho Chehab INIT_LIST_HEAD(&sp->ready_list);
4040c0d06caSMauro Carvalho Chehab INIT_LIST_HEAD(&sp->idle_list);
4050c0d06caSMauro Carvalho Chehab }
4060c0d06caSMauro Carvalho Chehab
pvr2_stream_done(struct pvr2_stream * sp)4070c0d06caSMauro Carvalho Chehab static void pvr2_stream_done(struct pvr2_stream *sp)
4080c0d06caSMauro Carvalho Chehab {
4090c0d06caSMauro Carvalho Chehab mutex_lock(&sp->mutex); do {
4100c0d06caSMauro Carvalho Chehab pvr2_stream_internal_flush(sp);
4110c0d06caSMauro Carvalho Chehab pvr2_stream_buffer_count(sp, 0);
4120c0d06caSMauro Carvalho Chehab } while (0); mutex_unlock(&sp->mutex);
4130c0d06caSMauro Carvalho Chehab }
4140c0d06caSMauro Carvalho Chehab
buffer_complete(struct urb * urb)4150c0d06caSMauro Carvalho Chehab static void buffer_complete(struct urb *urb)
4160c0d06caSMauro Carvalho Chehab {
4170c0d06caSMauro Carvalho Chehab struct pvr2_buffer *bp = urb->context;
4180c0d06caSMauro Carvalho Chehab struct pvr2_stream *sp;
4190c0d06caSMauro Carvalho Chehab unsigned long irq_flags;
4200c0d06caSMauro Carvalho Chehab BUFFER_CHECK(bp);
4210c0d06caSMauro Carvalho Chehab sp = bp->stream;
4220c0d06caSMauro Carvalho Chehab bp->used_count = 0;
4230c0d06caSMauro Carvalho Chehab bp->status = 0;
4240c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_BUF_FLOW,
4250c0d06caSMauro Carvalho Chehab "/*---TRACE_FLOW---*/ bufferComplete %p stat=%d cnt=%d",
4260c0d06caSMauro Carvalho Chehab bp, urb->status, urb->actual_length);
4270c0d06caSMauro Carvalho Chehab spin_lock_irqsave(&sp->list_lock, irq_flags);
4280c0d06caSMauro Carvalho Chehab if ((!(urb->status)) ||
4290c0d06caSMauro Carvalho Chehab (urb->status == -ENOENT) ||
4300c0d06caSMauro Carvalho Chehab (urb->status == -ECONNRESET) ||
4310c0d06caSMauro Carvalho Chehab (urb->status == -ESHUTDOWN)) {
4320c0d06caSMauro Carvalho Chehab (sp->buffers_processed)++;
4330c0d06caSMauro Carvalho Chehab sp->bytes_processed += urb->actual_length;
4340c0d06caSMauro Carvalho Chehab bp->used_count = urb->actual_length;
4350c0d06caSMauro Carvalho Chehab if (sp->fail_count) {
4360c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_TOLERANCE,
43796292c89SMauro Carvalho Chehab "stream %p transfer ok - fail count reset",
43896292c89SMauro Carvalho Chehab sp);
4390c0d06caSMauro Carvalho Chehab sp->fail_count = 0;
4400c0d06caSMauro Carvalho Chehab }
4410c0d06caSMauro Carvalho Chehab } else if (sp->fail_count < sp->fail_tolerance) {
4420c0d06caSMauro Carvalho Chehab // We can tolerate this error, because we're below the
4430c0d06caSMauro Carvalho Chehab // threshold...
4440c0d06caSMauro Carvalho Chehab (sp->fail_count)++;
4450c0d06caSMauro Carvalho Chehab (sp->buffers_failed)++;
4460c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_TOLERANCE,
44796292c89SMauro Carvalho Chehab "stream %p ignoring error %d - fail count increased to %u",
4480c0d06caSMauro Carvalho Chehab sp, urb->status, sp->fail_count);
4490c0d06caSMauro Carvalho Chehab } else {
4500c0d06caSMauro Carvalho Chehab (sp->buffers_failed)++;
4510c0d06caSMauro Carvalho Chehab bp->status = urb->status;
4520c0d06caSMauro Carvalho Chehab }
4530c0d06caSMauro Carvalho Chehab spin_unlock_irqrestore(&sp->list_lock, irq_flags);
4540c0d06caSMauro Carvalho Chehab pvr2_buffer_set_ready(bp);
45519409c89SMauro Carvalho Chehab if (sp->callback_func) {
4560c0d06caSMauro Carvalho Chehab sp->callback_func(sp->callback_data);
4570c0d06caSMauro Carvalho Chehab }
4580c0d06caSMauro Carvalho Chehab }
4590c0d06caSMauro Carvalho Chehab
pvr2_stream_create(void)4600c0d06caSMauro Carvalho Chehab struct pvr2_stream *pvr2_stream_create(void)
4610c0d06caSMauro Carvalho Chehab {
4620c0d06caSMauro Carvalho Chehab struct pvr2_stream *sp;
4630c0d06caSMauro Carvalho Chehab sp = kzalloc(sizeof(*sp), GFP_KERNEL);
4640c0d06caSMauro Carvalho Chehab if (!sp) return sp;
4650c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_INIT, "pvr2_stream_create: sp=%p", sp);
4660c0d06caSMauro Carvalho Chehab pvr2_stream_init(sp);
4670c0d06caSMauro Carvalho Chehab return sp;
4680c0d06caSMauro Carvalho Chehab }
4690c0d06caSMauro Carvalho Chehab
pvr2_stream_destroy(struct pvr2_stream * sp)4700c0d06caSMauro Carvalho Chehab void pvr2_stream_destroy(struct pvr2_stream *sp)
4710c0d06caSMauro Carvalho Chehab {
4720c0d06caSMauro Carvalho Chehab if (!sp) return;
4730c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_INIT, "pvr2_stream_destroy: sp=%p", sp);
4740c0d06caSMauro Carvalho Chehab pvr2_stream_done(sp);
4750c0d06caSMauro Carvalho Chehab kfree(sp);
4760c0d06caSMauro Carvalho Chehab }
4770c0d06caSMauro Carvalho Chehab
pvr2_stream_setup(struct pvr2_stream * sp,struct usb_device * dev,int endpoint,unsigned int tolerance)4780c0d06caSMauro Carvalho Chehab void pvr2_stream_setup(struct pvr2_stream *sp,
4790c0d06caSMauro Carvalho Chehab struct usb_device *dev,
4800c0d06caSMauro Carvalho Chehab int endpoint,
4810c0d06caSMauro Carvalho Chehab unsigned int tolerance)
4820c0d06caSMauro Carvalho Chehab {
4830c0d06caSMauro Carvalho Chehab mutex_lock(&sp->mutex); do {
4840c0d06caSMauro Carvalho Chehab pvr2_stream_internal_flush(sp);
4850c0d06caSMauro Carvalho Chehab sp->dev = dev;
4860c0d06caSMauro Carvalho Chehab sp->endpoint = endpoint;
4870c0d06caSMauro Carvalho Chehab sp->fail_tolerance = tolerance;
4880c0d06caSMauro Carvalho Chehab } while (0); mutex_unlock(&sp->mutex);
4890c0d06caSMauro Carvalho Chehab }
4900c0d06caSMauro Carvalho Chehab
pvr2_stream_set_callback(struct pvr2_stream * sp,pvr2_stream_callback func,void * data)4910c0d06caSMauro Carvalho Chehab void pvr2_stream_set_callback(struct pvr2_stream *sp,
4920c0d06caSMauro Carvalho Chehab pvr2_stream_callback func,
4930c0d06caSMauro Carvalho Chehab void *data)
4940c0d06caSMauro Carvalho Chehab {
4950c0d06caSMauro Carvalho Chehab unsigned long irq_flags;
496f419edd4SMauro Carvalho Chehab mutex_lock(&sp->mutex);
497f419edd4SMauro Carvalho Chehab do {
4980c0d06caSMauro Carvalho Chehab spin_lock_irqsave(&sp->list_lock, irq_flags);
4990c0d06caSMauro Carvalho Chehab sp->callback_data = data;
5000c0d06caSMauro Carvalho Chehab sp->callback_func = func;
5010c0d06caSMauro Carvalho Chehab spin_unlock_irqrestore(&sp->list_lock, irq_flags);
502f419edd4SMauro Carvalho Chehab } while (0);
503f419edd4SMauro Carvalho Chehab mutex_unlock(&sp->mutex);
5040c0d06caSMauro Carvalho Chehab }
5050c0d06caSMauro Carvalho Chehab
pvr2_stream_get_stats(struct pvr2_stream * sp,struct pvr2_stream_stats * stats,int zero_counts)5060c0d06caSMauro Carvalho Chehab void pvr2_stream_get_stats(struct pvr2_stream *sp,
5070c0d06caSMauro Carvalho Chehab struct pvr2_stream_stats *stats,
5080c0d06caSMauro Carvalho Chehab int zero_counts)
5090c0d06caSMauro Carvalho Chehab {
5100c0d06caSMauro Carvalho Chehab unsigned long irq_flags;
5110c0d06caSMauro Carvalho Chehab spin_lock_irqsave(&sp->list_lock, irq_flags);
5120c0d06caSMauro Carvalho Chehab if (stats) {
5130c0d06caSMauro Carvalho Chehab stats->buffers_in_queue = sp->q_count;
5140c0d06caSMauro Carvalho Chehab stats->buffers_in_idle = sp->i_count;
5150c0d06caSMauro Carvalho Chehab stats->buffers_in_ready = sp->r_count;
5160c0d06caSMauro Carvalho Chehab stats->buffers_processed = sp->buffers_processed;
5170c0d06caSMauro Carvalho Chehab stats->buffers_failed = sp->buffers_failed;
5180c0d06caSMauro Carvalho Chehab stats->bytes_processed = sp->bytes_processed;
5190c0d06caSMauro Carvalho Chehab }
5200c0d06caSMauro Carvalho Chehab if (zero_counts) {
5210c0d06caSMauro Carvalho Chehab sp->buffers_processed = 0;
5220c0d06caSMauro Carvalho Chehab sp->buffers_failed = 0;
5230c0d06caSMauro Carvalho Chehab sp->bytes_processed = 0;
5240c0d06caSMauro Carvalho Chehab }
5250c0d06caSMauro Carvalho Chehab spin_unlock_irqrestore(&sp->list_lock, irq_flags);
5260c0d06caSMauro Carvalho Chehab }
5270c0d06caSMauro Carvalho Chehab
5280c0d06caSMauro Carvalho Chehab /* Query / set the nominal buffer count */
pvr2_stream_get_buffer_count(struct pvr2_stream * sp)5290c0d06caSMauro Carvalho Chehab int pvr2_stream_get_buffer_count(struct pvr2_stream *sp)
5300c0d06caSMauro Carvalho Chehab {
5310c0d06caSMauro Carvalho Chehab return sp->buffer_target_count;
5320c0d06caSMauro Carvalho Chehab }
5330c0d06caSMauro Carvalho Chehab
pvr2_stream_set_buffer_count(struct pvr2_stream * sp,unsigned int cnt)5340c0d06caSMauro Carvalho Chehab int pvr2_stream_set_buffer_count(struct pvr2_stream *sp, unsigned int cnt)
5350c0d06caSMauro Carvalho Chehab {
5360c0d06caSMauro Carvalho Chehab int ret;
5370c0d06caSMauro Carvalho Chehab if (sp->buffer_target_count == cnt) return 0;
538f419edd4SMauro Carvalho Chehab mutex_lock(&sp->mutex);
539f419edd4SMauro Carvalho Chehab do {
5400c0d06caSMauro Carvalho Chehab sp->buffer_target_count = cnt;
5410c0d06caSMauro Carvalho Chehab ret = pvr2_stream_achieve_buffer_count(sp);
542f419edd4SMauro Carvalho Chehab } while (0);
543f419edd4SMauro Carvalho Chehab mutex_unlock(&sp->mutex);
5440c0d06caSMauro Carvalho Chehab return ret;
5450c0d06caSMauro Carvalho Chehab }
5460c0d06caSMauro Carvalho Chehab
pvr2_stream_get_idle_buffer(struct pvr2_stream * sp)5470c0d06caSMauro Carvalho Chehab struct pvr2_buffer *pvr2_stream_get_idle_buffer(struct pvr2_stream *sp)
5480c0d06caSMauro Carvalho Chehab {
5490c0d06caSMauro Carvalho Chehab struct list_head *lp = sp->idle_list.next;
5500c0d06caSMauro Carvalho Chehab if (lp == &sp->idle_list) return NULL;
5510c0d06caSMauro Carvalho Chehab return list_entry(lp, struct pvr2_buffer, list_overhead);
5520c0d06caSMauro Carvalho Chehab }
5530c0d06caSMauro Carvalho Chehab
pvr2_stream_get_ready_buffer(struct pvr2_stream * sp)5540c0d06caSMauro Carvalho Chehab struct pvr2_buffer *pvr2_stream_get_ready_buffer(struct pvr2_stream *sp)
5550c0d06caSMauro Carvalho Chehab {
5560c0d06caSMauro Carvalho Chehab struct list_head *lp = sp->ready_list.next;
5570c0d06caSMauro Carvalho Chehab if (lp == &sp->ready_list) return NULL;
5580c0d06caSMauro Carvalho Chehab return list_entry(lp, struct pvr2_buffer, list_overhead);
5590c0d06caSMauro Carvalho Chehab }
5600c0d06caSMauro Carvalho Chehab
pvr2_stream_get_buffer(struct pvr2_stream * sp,int id)5610c0d06caSMauro Carvalho Chehab struct pvr2_buffer *pvr2_stream_get_buffer(struct pvr2_stream *sp, int id)
5620c0d06caSMauro Carvalho Chehab {
5630c0d06caSMauro Carvalho Chehab if (id < 0) return NULL;
5640c0d06caSMauro Carvalho Chehab if (id >= sp->buffer_total_count) return NULL;
5650c0d06caSMauro Carvalho Chehab return sp->buffers[id];
5660c0d06caSMauro Carvalho Chehab }
5670c0d06caSMauro Carvalho Chehab
pvr2_stream_get_ready_count(struct pvr2_stream * sp)5680c0d06caSMauro Carvalho Chehab int pvr2_stream_get_ready_count(struct pvr2_stream *sp)
5690c0d06caSMauro Carvalho Chehab {
5700c0d06caSMauro Carvalho Chehab return sp->r_count;
5710c0d06caSMauro Carvalho Chehab }
5720c0d06caSMauro Carvalho Chehab
pvr2_stream_kill(struct pvr2_stream * sp)5730c0d06caSMauro Carvalho Chehab void pvr2_stream_kill(struct pvr2_stream *sp)
5740c0d06caSMauro Carvalho Chehab {
5750c0d06caSMauro Carvalho Chehab struct pvr2_buffer *bp;
576f419edd4SMauro Carvalho Chehab mutex_lock(&sp->mutex);
577f419edd4SMauro Carvalho Chehab do {
5780c0d06caSMauro Carvalho Chehab pvr2_stream_internal_flush(sp);
5790c0d06caSMauro Carvalho Chehab while ((bp = pvr2_stream_get_ready_buffer(sp)) != NULL) {
5800c0d06caSMauro Carvalho Chehab pvr2_buffer_set_idle(bp);
5810c0d06caSMauro Carvalho Chehab }
5820c0d06caSMauro Carvalho Chehab if (sp->buffer_total_count != sp->buffer_target_count) {
5830c0d06caSMauro Carvalho Chehab pvr2_stream_achieve_buffer_count(sp);
5840c0d06caSMauro Carvalho Chehab }
585f419edd4SMauro Carvalho Chehab } while (0);
586f419edd4SMauro Carvalho Chehab mutex_unlock(&sp->mutex);
5870c0d06caSMauro Carvalho Chehab }
5880c0d06caSMauro Carvalho Chehab
pvr2_buffer_queue(struct pvr2_buffer * bp)5890c0d06caSMauro Carvalho Chehab int pvr2_buffer_queue(struct pvr2_buffer *bp)
5900c0d06caSMauro Carvalho Chehab {
5910c0d06caSMauro Carvalho Chehab #undef SEED_BUFFER
5920c0d06caSMauro Carvalho Chehab #ifdef SEED_BUFFER
5930c0d06caSMauro Carvalho Chehab unsigned int idx;
5940c0d06caSMauro Carvalho Chehab unsigned int val;
5950c0d06caSMauro Carvalho Chehab #endif
5960c0d06caSMauro Carvalho Chehab int ret = 0;
5970c0d06caSMauro Carvalho Chehab struct pvr2_stream *sp;
5980c0d06caSMauro Carvalho Chehab if (!bp) return -EINVAL;
5990c0d06caSMauro Carvalho Chehab sp = bp->stream;
600f419edd4SMauro Carvalho Chehab mutex_lock(&sp->mutex);
601f419edd4SMauro Carvalho Chehab do {
6020c0d06caSMauro Carvalho Chehab pvr2_buffer_wipe(bp);
6030c0d06caSMauro Carvalho Chehab if (!sp->dev) {
6040c0d06caSMauro Carvalho Chehab ret = -EIO;
6050c0d06caSMauro Carvalho Chehab break;
6060c0d06caSMauro Carvalho Chehab }
6070c0d06caSMauro Carvalho Chehab pvr2_buffer_set_queued(bp);
6080c0d06caSMauro Carvalho Chehab #ifdef SEED_BUFFER
6090c0d06caSMauro Carvalho Chehab for (idx = 0; idx < (bp->max_count) / 4; idx++) {
6100c0d06caSMauro Carvalho Chehab val = bp->id << 24;
6110c0d06caSMauro Carvalho Chehab val |= idx;
6120c0d06caSMauro Carvalho Chehab ((unsigned int *)(bp->ptr))[idx] = val;
6130c0d06caSMauro Carvalho Chehab }
6140c0d06caSMauro Carvalho Chehab #endif
6150c0d06caSMauro Carvalho Chehab bp->status = -EINPROGRESS;
6160c0d06caSMauro Carvalho Chehab usb_fill_bulk_urb(bp->purb, // struct urb *urb
6170c0d06caSMauro Carvalho Chehab sp->dev, // struct usb_device *dev
6180c0d06caSMauro Carvalho Chehab // endpoint (below)
6190c0d06caSMauro Carvalho Chehab usb_rcvbulkpipe(sp->dev, sp->endpoint),
6200c0d06caSMauro Carvalho Chehab bp->ptr, // void *transfer_buffer
6210c0d06caSMauro Carvalho Chehab bp->max_count, // int buffer_length
6220c0d06caSMauro Carvalho Chehab buffer_complete,
6230c0d06caSMauro Carvalho Chehab bp);
6240c0d06caSMauro Carvalho Chehab usb_submit_urb(bp->purb, GFP_KERNEL);
625f419edd4SMauro Carvalho Chehab } while (0);
626f419edd4SMauro Carvalho Chehab mutex_unlock(&sp->mutex);
6270c0d06caSMauro Carvalho Chehab return ret;
6280c0d06caSMauro Carvalho Chehab }
6290c0d06caSMauro Carvalho Chehab
pvr2_buffer_set_buffer(struct pvr2_buffer * bp,void * ptr,unsigned int cnt)6300c0d06caSMauro Carvalho Chehab int pvr2_buffer_set_buffer(struct pvr2_buffer *bp, void *ptr, unsigned int cnt)
6310c0d06caSMauro Carvalho Chehab {
6320c0d06caSMauro Carvalho Chehab int ret = 0;
6330c0d06caSMauro Carvalho Chehab unsigned long irq_flags;
6340c0d06caSMauro Carvalho Chehab struct pvr2_stream *sp;
6350c0d06caSMauro Carvalho Chehab if (!bp) return -EINVAL;
6360c0d06caSMauro Carvalho Chehab sp = bp->stream;
637f419edd4SMauro Carvalho Chehab mutex_lock(&sp->mutex);
638f419edd4SMauro Carvalho Chehab do {
6390c0d06caSMauro Carvalho Chehab spin_lock_irqsave(&sp->list_lock, irq_flags);
6400c0d06caSMauro Carvalho Chehab if (bp->state != pvr2_buffer_state_idle) {
6410c0d06caSMauro Carvalho Chehab ret = -EPERM;
6420c0d06caSMauro Carvalho Chehab } else {
6430c0d06caSMauro Carvalho Chehab bp->ptr = ptr;
6440c0d06caSMauro Carvalho Chehab bp->stream->i_bcount -= bp->max_count;
6450c0d06caSMauro Carvalho Chehab bp->max_count = cnt;
6460c0d06caSMauro Carvalho Chehab bp->stream->i_bcount += bp->max_count;
6470c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_BUF_FLOW,
64896292c89SMauro Carvalho Chehab "/*---TRACE_FLOW---*/ bufferPool %8s cap cap=%07d cnt=%02d",
6490c0d06caSMauro Carvalho Chehab pvr2_buffer_state_decode(
6500c0d06caSMauro Carvalho Chehab pvr2_buffer_state_idle),
6510c0d06caSMauro Carvalho Chehab bp->stream->i_bcount, bp->stream->i_count);
6520c0d06caSMauro Carvalho Chehab }
6530c0d06caSMauro Carvalho Chehab spin_unlock_irqrestore(&sp->list_lock, irq_flags);
654f419edd4SMauro Carvalho Chehab } while (0);
655f419edd4SMauro Carvalho Chehab mutex_unlock(&sp->mutex);
6560c0d06caSMauro Carvalho Chehab return ret;
6570c0d06caSMauro Carvalho Chehab }
6580c0d06caSMauro Carvalho Chehab
pvr2_buffer_get_count(struct pvr2_buffer * bp)6590c0d06caSMauro Carvalho Chehab unsigned int pvr2_buffer_get_count(struct pvr2_buffer *bp)
6600c0d06caSMauro Carvalho Chehab {
6610c0d06caSMauro Carvalho Chehab return bp->used_count;
6620c0d06caSMauro Carvalho Chehab }
6630c0d06caSMauro Carvalho Chehab
pvr2_buffer_get_status(struct pvr2_buffer * bp)6640c0d06caSMauro Carvalho Chehab int pvr2_buffer_get_status(struct pvr2_buffer *bp)
6650c0d06caSMauro Carvalho Chehab {
6660c0d06caSMauro Carvalho Chehab return bp->status;
6670c0d06caSMauro Carvalho Chehab }
6680c0d06caSMauro Carvalho Chehab
pvr2_buffer_get_id(struct pvr2_buffer * bp)6690c0d06caSMauro Carvalho Chehab int pvr2_buffer_get_id(struct pvr2_buffer *bp)
6700c0d06caSMauro Carvalho Chehab {
6710c0d06caSMauro Carvalho Chehab return bp->id;
6720c0d06caSMauro Carvalho Chehab }
673