2008-04-28 16:24:33 -07:00
|
|
|
/*
|
|
|
|
* cx18 buffer queues
|
|
|
|
*
|
|
|
|
* Derived from ivtv-queue.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
|
2008-11-21 21:37:34 -07:00
|
|
|
* Copyright (C) 2008 Andy Walls <awalls@radix.net>
|
2008-04-28 16:24:33 -07:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
* 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cx18-driver.h"
|
|
|
|
#include "cx18-queue.h"
|
2009-04-15 16:45:10 -07:00
|
|
|
#include "cx18-streams.h"
|
2008-04-28 16:24:33 -07:00
|
|
|
#include "cx18-scb.h"
|
2009-11-08 19:45:24 -07:00
|
|
|
#include "cx18-io.h"
|
2008-04-28 16:24:33 -07:00
|
|
|
|
|
|
|
void cx18_buf_swap(struct cx18_buffer *buf)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < buf->bytesused; i += 4)
|
|
|
|
swab32s((u32 *)(buf->buf + i));
|
|
|
|
}
|
|
|
|
|
2009-11-08 19:45:24 -07:00
|
|
|
void _cx18_mdl_swap(struct cx18_mdl *mdl)
|
|
|
|
{
|
|
|
|
struct cx18_buffer *buf;
|
|
|
|
|
|
|
|
list_for_each_entry(buf, &mdl->buf_list, list) {
|
|
|
|
if (buf->bytesused == 0)
|
|
|
|
break;
|
|
|
|
cx18_buf_swap(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-28 16:24:33 -07:00
|
|
|
void cx18_queue_init(struct cx18_queue *q)
|
|
|
|
{
|
|
|
|
INIT_LIST_HEAD(&q->list);
|
2009-11-04 19:13:58 -07:00
|
|
|
atomic_set(&q->depth, 0);
|
2008-04-28 16:24:33 -07:00
|
|
|
q->bytesused = 0;
|
|
|
|
}
|
|
|
|
|
2009-11-08 19:45:24 -07:00
|
|
|
struct cx18_queue *_cx18_enqueue(struct cx18_stream *s, struct cx18_mdl *mdl,
|
2008-12-08 19:02:45 -07:00
|
|
|
struct cx18_queue *q, int to_front)
|
2008-04-28 16:24:33 -07:00
|
|
|
{
|
2009-11-08 19:45:24 -07:00
|
|
|
/* clear the mdl if it is not to be enqueued to the full queue */
|
2008-12-08 19:02:45 -07:00
|
|
|
if (q != &s->q_full) {
|
2009-11-08 19:45:24 -07:00
|
|
|
mdl->bytesused = 0;
|
|
|
|
mdl->readpos = 0;
|
|
|
|
mdl->m_flags = 0;
|
|
|
|
mdl->skipped = 0;
|
|
|
|
mdl->curr_buf = NULL;
|
2008-04-28 16:24:33 -07:00
|
|
|
}
|
2008-12-08 19:02:45 -07:00
|
|
|
|
2008-12-14 14:52:12 -07:00
|
|
|
/* q_busy is restricted to a max buffer count imposed by firmware */
|
|
|
|
if (q == &s->q_busy &&
|
2009-11-04 19:13:58 -07:00
|
|
|
atomic_read(&q->depth) >= CX18_MAX_FW_MDLS_PER_STREAM)
|
2008-12-08 19:02:45 -07:00
|
|
|
q = &s->q_free;
|
|
|
|
|
2009-04-13 19:08:00 -07:00
|
|
|
spin_lock(&q->lock);
|
|
|
|
|
2008-11-27 20:04:21 -07:00
|
|
|
if (to_front)
|
2009-11-08 19:45:24 -07:00
|
|
|
list_add(&mdl->list, &q->list); /* LIFO */
|
2008-11-27 20:04:21 -07:00
|
|
|
else
|
2009-11-08 19:45:24 -07:00
|
|
|
list_add_tail(&mdl->list, &q->list); /* FIFO */
|
|
|
|
q->bytesused += mdl->bytesused - mdl->readpos;
|
2009-11-04 19:13:58 -07:00
|
|
|
atomic_inc(&q->depth);
|
2008-12-08 19:02:45 -07:00
|
|
|
|
2009-04-13 19:08:00 -07:00
|
|
|
spin_unlock(&q->lock);
|
2008-12-08 19:02:45 -07:00
|
|
|
return q;
|
2008-04-28 16:24:33 -07:00
|
|
|
}
|
|
|
|
|
2009-11-08 19:45:24 -07:00
|
|
|
struct cx18_mdl *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q)
|
2008-04-28 16:24:33 -07:00
|
|
|
{
|
2009-11-08 19:45:24 -07:00
|
|
|
struct cx18_mdl *mdl = NULL;
|
2008-04-28 16:24:33 -07:00
|
|
|
|
2009-04-13 19:08:00 -07:00
|
|
|
spin_lock(&q->lock);
|
2008-04-28 16:24:33 -07:00
|
|
|
if (!list_empty(&q->list)) {
|
2009-11-08 19:45:24 -07:00
|
|
|
mdl = list_first_entry(&q->list, struct cx18_mdl, list);
|
|
|
|
list_del_init(&mdl->list);
|
|
|
|
q->bytesused -= mdl->bytesused - mdl->readpos;
|
|
|
|
mdl->skipped = 0;
|
2009-11-04 19:13:58 -07:00
|
|
|
atomic_dec(&q->depth);
|
2008-04-28 16:24:33 -07:00
|
|
|
}
|
2009-04-13 19:08:00 -07:00
|
|
|
spin_unlock(&q->lock);
|
2009-11-08 19:45:24 -07:00
|
|
|
return mdl;
|
|
|
|
}
|
|
|
|
|
2009-11-10 20:57:16 -07:00
|
|
|
static void _cx18_mdl_update_bufs_for_cpu(struct cx18_stream *s,
|
|
|
|
struct cx18_mdl *mdl)
|
2009-11-08 19:45:24 -07:00
|
|
|
{
|
|
|
|
struct cx18_buffer *buf;
|
|
|
|
u32 buf_size = s->buf_size;
|
|
|
|
u32 bytesused = mdl->bytesused;
|
|
|
|
|
|
|
|
list_for_each_entry(buf, &mdl->buf_list, list) {
|
|
|
|
buf->readpos = 0;
|
|
|
|
if (bytesused >= buf_size) {
|
|
|
|
buf->bytesused = buf_size;
|
|
|
|
bytesused -= buf_size;
|
|
|
|
} else {
|
|
|
|
buf->bytesused = bytesused;
|
|
|
|
bytesused = 0;
|
|
|
|
}
|
2009-11-10 20:57:16 -07:00
|
|
|
cx18_buf_sync_for_cpu(s, buf);
|
2009-11-08 19:45:24 -07:00
|
|
|
}
|
2008-04-28 16:24:33 -07:00
|
|
|
}
|
|
|
|
|
2009-11-10 20:57:16 -07:00
|
|
|
static inline void cx18_mdl_update_bufs_for_cpu(struct cx18_stream *s,
|
|
|
|
struct cx18_mdl *mdl)
|
2009-11-08 19:45:24 -07:00
|
|
|
{
|
|
|
|
struct cx18_buffer *buf;
|
|
|
|
|
|
|
|
if (list_is_singular(&mdl->buf_list)) {
|
|
|
|
buf = list_first_entry(&mdl->buf_list, struct cx18_buffer,
|
|
|
|
list);
|
|
|
|
buf->bytesused = mdl->bytesused;
|
|
|
|
buf->readpos = 0;
|
2009-11-10 20:57:16 -07:00
|
|
|
cx18_buf_sync_for_cpu(s, buf);
|
2009-11-08 19:45:24 -07:00
|
|
|
} else {
|
2009-11-10 20:57:16 -07:00
|
|
|
_cx18_mdl_update_bufs_for_cpu(s, mdl);
|
2009-11-08 19:45:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct cx18_mdl *cx18_queue_get_mdl(struct cx18_stream *s, u32 id,
|
2008-04-28 16:24:33 -07:00
|
|
|
u32 bytesused)
|
|
|
|
{
|
|
|
|
struct cx18 *cx = s->cx;
|
2009-11-08 19:45:24 -07:00
|
|
|
struct cx18_mdl *mdl;
|
|
|
|
struct cx18_mdl *tmp;
|
|
|
|
struct cx18_mdl *ret = NULL;
|
2009-04-13 19:08:00 -07:00
|
|
|
LIST_HEAD(sweep_up);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't have to acquire multiple q locks here, because we are
|
|
|
|
* serialized by the single threaded work handler.
|
2009-11-08 19:45:24 -07:00
|
|
|
* MDLs from the firmware will thus remain in order as
|
2009-04-13 19:08:00 -07:00
|
|
|
* they are moved from q_busy to q_full or to the dvb ring buffer.
|
|
|
|
*/
|
|
|
|
spin_lock(&s->q_busy.lock);
|
2009-11-08 19:45:24 -07:00
|
|
|
list_for_each_entry_safe(mdl, tmp, &s->q_busy.list, list) {
|
2009-04-13 19:08:00 -07:00
|
|
|
/*
|
|
|
|
* We should find what the firmware told us is done,
|
|
|
|
* right at the front of the queue. If we don't, we likely have
|
2009-11-08 19:45:24 -07:00
|
|
|
* missed an mdl done message from the firmware.
|
|
|
|
* Once we skip an mdl repeatedly, relative to the size of
|
2009-04-13 19:08:00 -07:00
|
|
|
* q_busy, we have high confidence we've missed it.
|
|
|
|
*/
|
2009-11-08 19:45:24 -07:00
|
|
|
if (mdl->id != id) {
|
|
|
|
mdl->skipped++;
|
|
|
|
if (mdl->skipped >= atomic_read(&s->q_busy.depth)-1) {
|
|
|
|
/* mdl must have fallen out of rotation */
|
|
|
|
CX18_WARN("Skipped %s, MDL %d, %d "
|
2008-11-18 21:24:33 -07:00
|
|
|
"times - it must have dropped out of "
|
2009-11-08 19:45:24 -07:00
|
|
|
"rotation\n", s->name, mdl->id,
|
|
|
|
mdl->skipped);
|
2009-04-13 19:08:00 -07:00
|
|
|
/* Sweep it up to put it back into rotation */
|
2009-11-08 19:45:24 -07:00
|
|
|
list_move_tail(&mdl->list, &sweep_up);
|
2009-11-04 19:13:58 -07:00
|
|
|
atomic_dec(&s->q_busy.depth);
|
2008-11-18 21:24:33 -07:00
|
|
|
}
|
2008-04-28 16:24:33 -07:00
|
|
|
continue;
|
2008-11-15 21:38:19 -07:00
|
|
|
}
|
2009-04-13 19:08:00 -07:00
|
|
|
/*
|
2009-11-08 19:45:24 -07:00
|
|
|
* We pull the desired mdl off of the queue here. Something
|
2009-04-13 19:08:00 -07:00
|
|
|
* will have to put it back on a queue later.
|
|
|
|
*/
|
2009-11-08 19:45:24 -07:00
|
|
|
list_del_init(&mdl->list);
|
2009-11-04 19:13:58 -07:00
|
|
|
atomic_dec(&s->q_busy.depth);
|
2009-11-08 19:45:24 -07:00
|
|
|
ret = mdl;
|
2008-11-18 21:24:33 -07:00
|
|
|
break;
|
2008-04-28 16:24:33 -07:00
|
|
|
}
|
2009-04-13 19:08:00 -07:00
|
|
|
spin_unlock(&s->q_busy.lock);
|
|
|
|
|
|
|
|
/*
|
2009-11-08 19:45:24 -07:00
|
|
|
* We found the mdl for which we were looking. Get it ready for
|
2009-04-13 19:08:00 -07:00
|
|
|
* the caller to put on q_full or in the dvb ring buffer.
|
|
|
|
*/
|
|
|
|
if (ret != NULL) {
|
|
|
|
ret->bytesused = bytesused;
|
|
|
|
ret->skipped = 0;
|
2009-11-08 19:45:24 -07:00
|
|
|
/* 0'ed readpos, m_flags & curr_buf when mdl went on q_busy */
|
2009-11-10 20:57:16 -07:00
|
|
|
cx18_mdl_update_bufs_for_cpu(s, ret);
|
2009-04-13 19:08:00 -07:00
|
|
|
if (s->type != CX18_ENC_STREAM_TYPE_TS)
|
2009-11-08 19:45:24 -07:00
|
|
|
set_bit(CX18_F_M_NEED_SWAP, &ret->m_flags);
|
2009-04-13 19:08:00 -07:00
|
|
|
}
|
|
|
|
|
2009-11-08 19:45:24 -07:00
|
|
|
/* Put any mdls the firmware is ignoring back into normal rotation */
|
|
|
|
list_for_each_entry_safe(mdl, tmp, &sweep_up, list) {
|
|
|
|
list_del_init(&mdl->list);
|
|
|
|
cx18_enqueue(s, mdl, &s->q_free);
|
2009-04-13 19:08:00 -07:00
|
|
|
}
|
2008-11-18 21:24:33 -07:00
|
|
|
return ret;
|
2008-04-28 16:24:33 -07:00
|
|
|
}
|
|
|
|
|
2009-11-08 19:45:24 -07:00
|
|
|
/* Move all mdls of a queue, while flushing the mdl */
|
|
|
|
static void cx18_queue_flush(struct cx18_stream *s,
|
|
|
|
struct cx18_queue *q_src, struct cx18_queue *q_dst)
|
2008-04-28 16:24:33 -07:00
|
|
|
{
|
2009-11-08 19:45:24 -07:00
|
|
|
struct cx18_mdl *mdl;
|
2008-04-28 16:24:33 -07:00
|
|
|
|
2009-11-08 19:45:24 -07:00
|
|
|
/* It only makes sense to flush to q_free or q_idle */
|
|
|
|
if (q_src == q_dst || q_dst == &s->q_full || q_dst == &s->q_busy)
|
2008-09-03 13:11:54 -07:00
|
|
|
return;
|
2008-04-28 16:24:33 -07:00
|
|
|
|
2009-11-08 19:45:24 -07:00
|
|
|
spin_lock(&q_src->lock);
|
|
|
|
spin_lock(&q_dst->lock);
|
|
|
|
while (!list_empty(&q_src->list)) {
|
|
|
|
mdl = list_first_entry(&q_src->list, struct cx18_mdl, list);
|
|
|
|
list_move_tail(&mdl->list, &q_dst->list);
|
|
|
|
mdl->bytesused = 0;
|
|
|
|
mdl->readpos = 0;
|
|
|
|
mdl->m_flags = 0;
|
|
|
|
mdl->skipped = 0;
|
|
|
|
mdl->curr_buf = NULL;
|
|
|
|
atomic_inc(&q_dst->depth);
|
2008-04-28 16:24:33 -07:00
|
|
|
}
|
2009-11-08 19:45:24 -07:00
|
|
|
cx18_queue_init(q_src);
|
|
|
|
spin_unlock(&q_src->lock);
|
|
|
|
spin_unlock(&q_dst->lock);
|
2008-04-28 16:24:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void cx18_flush_queues(struct cx18_stream *s)
|
|
|
|
{
|
2009-11-08 19:45:24 -07:00
|
|
|
cx18_queue_flush(s, &s->q_busy, &s->q_free);
|
|
|
|
cx18_queue_flush(s, &s->q_full, &s->q_free);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note, s->buf_pool is not protected by a lock,
|
|
|
|
* the stream better not have *anything* going on when calling this
|
|
|
|
*/
|
|
|
|
void cx18_unload_queues(struct cx18_stream *s)
|
|
|
|
{
|
|
|
|
struct cx18_queue *q_idle = &s->q_idle;
|
|
|
|
struct cx18_mdl *mdl;
|
|
|
|
struct cx18_buffer *buf;
|
|
|
|
|
|
|
|
/* Move all MDLS to q_idle */
|
|
|
|
cx18_queue_flush(s, &s->q_busy, q_idle);
|
|
|
|
cx18_queue_flush(s, &s->q_full, q_idle);
|
|
|
|
cx18_queue_flush(s, &s->q_free, q_idle);
|
|
|
|
|
|
|
|
/* Reset MDL id's and move all buffers back to the stream's buf_pool */
|
|
|
|
spin_lock(&q_idle->lock);
|
|
|
|
list_for_each_entry(mdl, &q_idle->list, list) {
|
|
|
|
while (!list_empty(&mdl->buf_list)) {
|
|
|
|
buf = list_first_entry(&mdl->buf_list,
|
|
|
|
struct cx18_buffer, list);
|
|
|
|
list_move_tail(&buf->list, &s->buf_pool);
|
|
|
|
buf->bytesused = 0;
|
|
|
|
buf->readpos = 0;
|
|
|
|
}
|
|
|
|
mdl->id = s->mdl_base_idx; /* reset id to a "safe" value */
|
|
|
|
/* all other mdl fields were cleared by cx18_queue_flush() */
|
|
|
|
}
|
|
|
|
spin_unlock(&q_idle->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note, s->buf_pool is not protected by a lock,
|
|
|
|
* the stream better not have *anything* going on when calling this
|
|
|
|
*/
|
|
|
|
void cx18_load_queues(struct cx18_stream *s)
|
|
|
|
{
|
|
|
|
struct cx18 *cx = s->cx;
|
|
|
|
struct cx18_mdl *mdl;
|
|
|
|
struct cx18_buffer *buf;
|
|
|
|
int mdl_id;
|
|
|
|
int i;
|
2009-11-10 19:28:30 -07:00
|
|
|
u32 partial_buf_size;
|
2009-11-08 19:45:24 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Attach buffers to MDLs, give the MDLs ids, and add MDLs to q_free
|
|
|
|
* Excess MDLs are left on q_idle
|
|
|
|
* Excess buffers are left in buf_pool and/or on an MDL in q_idle
|
|
|
|
*/
|
|
|
|
mdl_id = s->mdl_base_idx;
|
|
|
|
for (mdl = cx18_dequeue(s, &s->q_idle), i = s->bufs_per_mdl;
|
|
|
|
mdl != NULL && i == s->bufs_per_mdl;
|
|
|
|
mdl = cx18_dequeue(s, &s->q_idle)) {
|
|
|
|
|
|
|
|
mdl->id = mdl_id;
|
|
|
|
|
|
|
|
for (i = 0; i < s->bufs_per_mdl; i++) {
|
|
|
|
if (list_empty(&s->buf_pool))
|
|
|
|
break;
|
|
|
|
|
|
|
|
buf = list_first_entry(&s->buf_pool, struct cx18_buffer,
|
|
|
|
list);
|
|
|
|
list_move_tail(&buf->list, &mdl->buf_list);
|
|
|
|
|
|
|
|
/* update the firmware's MDL array with this buffer */
|
|
|
|
cx18_writel(cx, buf->dma_handle,
|
|
|
|
&cx->scb->cpu_mdl[mdl_id + i].paddr);
|
|
|
|
cx18_writel(cx, s->buf_size,
|
|
|
|
&cx->scb->cpu_mdl[mdl_id + i].length);
|
|
|
|
}
|
|
|
|
|
2009-11-10 19:28:30 -07:00
|
|
|
if (i == s->bufs_per_mdl) {
|
|
|
|
/*
|
|
|
|
* The encoder doesn't honor s->mdl_size. So in the
|
|
|
|
* case of a non-integral number of buffers to meet
|
|
|
|
* mdl_size, we lie about the size of the last buffer
|
|
|
|
* in the MDL to get the encoder to really only send
|
|
|
|
* us mdl_size bytes per MDL transfer.
|
|
|
|
*/
|
|
|
|
partial_buf_size = s->mdl_size % s->buf_size;
|
|
|
|
if (partial_buf_size) {
|
|
|
|
cx18_writel(cx, partial_buf_size,
|
|
|
|
&cx->scb->cpu_mdl[mdl_id + i - 1].length);
|
|
|
|
}
|
2009-11-08 19:45:24 -07:00
|
|
|
cx18_enqueue(s, mdl, &s->q_free);
|
2009-11-10 19:28:30 -07:00
|
|
|
} else {
|
|
|
|
/* Not enough buffers for this MDL; we won't use it */
|
|
|
|
cx18_push(s, mdl, &s->q_idle);
|
|
|
|
}
|
2009-11-08 19:45:24 -07:00
|
|
|
mdl_id += i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _cx18_mdl_sync_for_device(struct cx18_stream *s, struct cx18_mdl *mdl)
|
|
|
|
{
|
|
|
|
int dma = s->dma;
|
|
|
|
u32 buf_size = s->buf_size;
|
|
|
|
struct pci_dev *pci_dev = s->cx->pci_dev;
|
|
|
|
struct cx18_buffer *buf;
|
|
|
|
|
|
|
|
list_for_each_entry(buf, &mdl->buf_list, list)
|
|
|
|
pci_dma_sync_single_for_device(pci_dev, buf->dma_handle,
|
|
|
|
buf_size, dma);
|
2008-04-28 16:24:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int cx18_stream_alloc(struct cx18_stream *s)
|
|
|
|
{
|
|
|
|
struct cx18 *cx = s->cx;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (s->buffers == 0)
|
|
|
|
return 0;
|
|
|
|
|
2009-11-09 19:55:30 -07:00
|
|
|
CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers "
|
|
|
|
"(%d.%02d kB total)\n",
|
2008-04-28 16:24:33 -07:00
|
|
|
s->name, s->buffers, s->buf_size,
|
2009-11-09 19:55:30 -07:00
|
|
|
s->buffers * s->buf_size / 1024,
|
|
|
|
(s->buffers * s->buf_size * 100 / 1024) % 100);
|
2008-04-28 16:24:33 -07:00
|
|
|
|
2009-11-05 17:51:24 -07:00
|
|
|
if (((char __iomem *)&cx->scb->cpu_mdl[cx->free_mdl_idx + s->buffers] -
|
2008-09-03 13:11:54 -07:00
|
|
|
(char __iomem *)cx->scb) > SCB_RESERVED_SIZE) {
|
|
|
|
unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE -
|
|
|
|
((char __iomem *)cx->scb->cpu_mdl));
|
2008-04-28 16:24:33 -07:00
|
|
|
|
|
|
|
CX18_ERR("Too many buffers, cannot fit in SCB area\n");
|
|
|
|
CX18_ERR("Max buffers = %zd\n",
|
2009-11-04 18:33:33 -07:00
|
|
|
bufsz / sizeof(struct cx18_mdl_ent));
|
2008-04-28 16:24:33 -07:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2009-11-05 17:51:24 -07:00
|
|
|
s->mdl_base_idx = cx->free_mdl_idx;
|
2008-04-28 16:24:33 -07:00
|
|
|
|
2009-11-08 19:45:24 -07:00
|
|
|
/* allocate stream buffers and MDLs */
|
2008-04-28 16:24:33 -07:00
|
|
|
for (i = 0; i < s->buffers; i++) {
|
2009-11-08 19:45:24 -07:00
|
|
|
struct cx18_mdl *mdl;
|
|
|
|
struct cx18_buffer *buf;
|
2008-04-28 16:24:33 -07:00
|
|
|
|
2009-11-08 19:45:24 -07:00
|
|
|
/* 1 MDL per buffer to handle the worst & also default case */
|
|
|
|
mdl = kzalloc(sizeof(struct cx18_mdl), GFP_KERNEL|__GFP_NOWARN);
|
|
|
|
if (mdl == NULL)
|
2008-04-28 16:24:33 -07:00
|
|
|
break;
|
2009-11-08 19:45:24 -07:00
|
|
|
|
|
|
|
buf = kzalloc(sizeof(struct cx18_buffer),
|
|
|
|
GFP_KERNEL|__GFP_NOWARN);
|
|
|
|
if (buf == NULL) {
|
|
|
|
kfree(mdl);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-05-01 06:31:12 -07:00
|
|
|
buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN);
|
2008-04-28 16:24:33 -07:00
|
|
|
if (buf->buf == NULL) {
|
2009-11-08 19:45:24 -07:00
|
|
|
kfree(mdl);
|
2008-04-28 16:24:33 -07:00
|
|
|
kfree(buf);
|
|
|
|
break;
|
|
|
|
}
|
2009-11-08 19:45:24 -07:00
|
|
|
|
|
|
|
INIT_LIST_HEAD(&mdl->list);
|
|
|
|
INIT_LIST_HEAD(&mdl->buf_list);
|
|
|
|
mdl->id = s->mdl_base_idx; /* a somewhat safe value */
|
|
|
|
cx18_enqueue(s, mdl, &s->q_idle);
|
|
|
|
|
2008-04-28 16:24:33 -07:00
|
|
|
INIT_LIST_HEAD(&buf->list);
|
2009-01-10 17:54:39 -07:00
|
|
|
buf->dma_handle = pci_map_single(s->cx->pci_dev,
|
2008-04-28 16:24:33 -07:00
|
|
|
buf->buf, s->buf_size, s->dma);
|
|
|
|
cx18_buf_sync_for_cpu(s, buf);
|
2009-11-08 19:45:24 -07:00
|
|
|
list_add_tail(&buf->list, &s->buf_pool);
|
2008-04-28 16:24:33 -07:00
|
|
|
}
|
|
|
|
if (i == s->buffers) {
|
2009-11-05 17:51:24 -07:00
|
|
|
cx->free_mdl_idx += s->buffers;
|
2008-04-28 16:24:33 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
CX18_ERR("Couldn't allocate buffers for %s stream\n", s->name);
|
|
|
|
cx18_stream_free(s);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cx18_stream_free(struct cx18_stream *s)
|
|
|
|
{
|
2009-11-08 19:45:24 -07:00
|
|
|
struct cx18_mdl *mdl;
|
2008-04-28 16:24:33 -07:00
|
|
|
struct cx18_buffer *buf;
|
2009-12-30 21:35:08 -07:00
|
|
|
struct cx18 *cx = s->cx;
|
|
|
|
|
|
|
|
CX18_DEBUG_INFO("Deallocating buffers for %s stream\n", s->name);
|
2008-04-28 16:24:33 -07:00
|
|
|
|
2009-11-08 19:45:24 -07:00
|
|
|
/* move all buffers to buf_pool and all MDLs to q_idle */
|
|
|
|
cx18_unload_queues(s);
|
|
|
|
|
|
|
|
/* empty q_idle */
|
|
|
|
while ((mdl = cx18_dequeue(s, &s->q_idle)))
|
|
|
|
kfree(mdl);
|
|
|
|
|
|
|
|
/* empty buf_pool */
|
|
|
|
while (!list_empty(&s->buf_pool)) {
|
|
|
|
buf = list_first_entry(&s->buf_pool, struct cx18_buffer, list);
|
|
|
|
list_del_init(&buf->list);
|
2008-04-28 16:24:33 -07:00
|
|
|
|
2009-01-10 17:54:39 -07:00
|
|
|
pci_unmap_single(s->cx->pci_dev, buf->dma_handle,
|
2008-04-28 16:24:33 -07:00
|
|
|
s->buf_size, s->dma);
|
|
|
|
kfree(buf->buf);
|
|
|
|
kfree(buf);
|
|
|
|
}
|
|
|
|
}
|