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