4 * Derived from ivtv-queue.c
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 * Copyright (C) 2008 Andy Walls <awalls@radix.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #include "cx18-driver.h"
26 #include "cx18-streams.h"
27 #include "cx18-queue.h"
30 void cx18_buf_swap(struct cx18_buffer *buf)
34 for (i = 0; i < buf->bytesused; i += 4)
35 swab32s((u32 *)(buf->buf + i));
38 void cx18_queue_init(struct cx18_queue *q)
40 INIT_LIST_HEAD(&q->list);
41 atomic_set(&q->buffers, 0);
45 struct cx18_queue *_cx18_enqueue(struct cx18_stream *s, struct cx18_buffer *buf,
46 struct cx18_queue *q, int to_front)
48 /* clear the buffer if it is not to be enqueued to the full queue */
49 if (q != &s->q_full) {
56 mutex_lock(&s->qlock);
58 /* q_busy is restricted to 63 buffers to stay within firmware limits */
59 if (q == &s->q_busy && atomic_read(&q->buffers) >= 63)
63 list_add(&buf->list, &q->list); /* LIFO */
65 list_add_tail(&buf->list, &q->list); /* FIFO */
66 q->bytesused += buf->bytesused - buf->readpos;
67 atomic_inc(&q->buffers);
69 mutex_unlock(&s->qlock);
73 struct cx18_buffer *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q)
75 struct cx18_buffer *buf = NULL;
77 mutex_lock(&s->qlock);
78 if (!list_empty(&q->list)) {
79 buf = list_entry(q->list.next, struct cx18_buffer, list);
80 list_del_init(q->list.next);
81 q->bytesused -= buf->bytesused - buf->readpos;
83 atomic_dec(&q->buffers);
85 mutex_unlock(&s->qlock);
89 struct cx18_buffer *cx18_queue_get_buf(struct cx18_stream *s, u32 id,
92 struct cx18 *cx = s->cx;
93 struct cx18_buffer *buf;
94 struct cx18_buffer *ret = NULL;
95 struct list_head *p, *t;
97 mutex_lock(&s->qlock);
98 list_for_each_safe(p, t, &s->q_busy.list) {
99 buf = list_entry(p, struct cx18_buffer, list);
103 if (buf->skipped >= atomic_read(&s->q_busy.buffers)-1) {
104 /* buffer must have fallen out of rotation */
105 CX18_WARN("Skipped %s, buffer %d, %d "
106 "times - it must have dropped out of "
107 "rotation\n", s->name, buf->id,
109 /* move it to q_free */
110 list_move_tail(&buf->list, &s->q_free.list);
111 buf->bytesused = buf->readpos = buf->b_flags =
113 atomic_dec(&s->q_busy.buffers);
114 atomic_inc(&s->q_free.buffers);
119 buf->bytesused = bytesused;
120 /* Sync the buffer before we release the qlock */
121 cx18_buf_sync_for_cpu(s, buf);
122 if (s->type == CX18_ENC_STREAM_TYPE_TS) {
124 * TS doesn't use q_full. As we pull the buffer off of
125 * the queue here, the caller will have to put it back.
127 list_del_init(&buf->list);
129 /* Move buffer from q_busy to q_full */
130 list_move_tail(&buf->list, &s->q_full.list);
131 set_bit(CX18_F_B_NEED_BUF_SWAP, &buf->b_flags);
132 s->q_full.bytesused += buf->bytesused;
133 atomic_inc(&s->q_full.buffers);
135 atomic_dec(&s->q_busy.buffers);
140 mutex_unlock(&s->qlock);
144 /* Move all buffers of a queue to q_free, while flushing the buffers */
145 static void cx18_queue_flush(struct cx18_stream *s, struct cx18_queue *q)
147 struct cx18_buffer *buf;
152 mutex_lock(&s->qlock);
153 while (!list_empty(&q->list)) {
154 buf = list_entry(q->list.next, struct cx18_buffer, list);
155 list_move_tail(q->list.next, &s->q_free.list);
156 buf->bytesused = buf->readpos = buf->b_flags = buf->skipped = 0;
157 atomic_inc(&s->q_free.buffers);
160 mutex_unlock(&s->qlock);
163 void cx18_flush_queues(struct cx18_stream *s)
165 cx18_queue_flush(s, &s->q_busy);
166 cx18_queue_flush(s, &s->q_full);
169 int cx18_stream_alloc(struct cx18_stream *s)
171 struct cx18 *cx = s->cx;
177 CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers (%dkB total)\n",
178 s->name, s->buffers, s->buf_size,
179 s->buffers * s->buf_size / 1024);
181 if (((char __iomem *)&cx->scb->cpu_mdl[cx->mdl_offset + s->buffers] -
182 (char __iomem *)cx->scb) > SCB_RESERVED_SIZE) {
183 unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE -
184 ((char __iomem *)cx->scb->cpu_mdl));
186 CX18_ERR("Too many buffers, cannot fit in SCB area\n");
187 CX18_ERR("Max buffers = %zd\n",
188 bufsz / sizeof(struct cx18_mdl));
192 s->mdl_offset = cx->mdl_offset;
194 /* allocate stream buffers. Initially all buffers are in q_free. */
195 for (i = 0; i < s->buffers; i++) {
196 struct cx18_buffer *buf = kzalloc(sizeof(struct cx18_buffer),
197 GFP_KERNEL|__GFP_NOWARN);
201 buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN);
202 if (buf->buf == NULL) {
206 buf->id = cx->buffer_id++;
207 INIT_LIST_HEAD(&buf->list);
208 buf->dma_handle = pci_map_single(s->cx->dev,
209 buf->buf, s->buf_size, s->dma);
210 cx18_buf_sync_for_cpu(s, buf);
211 cx18_enqueue(s, buf, &s->q_free);
213 if (i == s->buffers) {
214 cx->mdl_offset += s->buffers;
217 CX18_ERR("Couldn't allocate buffers for %s stream\n", s->name);
222 void cx18_stream_free(struct cx18_stream *s)
224 struct cx18_buffer *buf;
226 /* move all buffers to q_free */
227 cx18_flush_queues(s);
230 while ((buf = cx18_dequeue(s, &s->q_free))) {
231 pci_unmap_single(s->cx->dev, buf->dma_handle,
232 s->buf_size, s->dma);