]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/media/video/cx18/cx18-queue.c
V4L/DVB (10279): cx18: Print driver version number when logging status
[net-next-2.6.git] / drivers / media / video / cx18 / cx18-queue.c
CommitLineData
1c1e45d1
HV
1/*
2 * cx18 buffer queues
3 *
4 * Derived from ivtv-queue.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
1ed9dcc8 7 * Copyright (C) 2008 Andy Walls <awalls@radix.net>
1c1e45d1
HV
8 *
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.
13 *
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.
18 *
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
22 * 02111-1307 USA
23 */
24
25#include "cx18-driver.h"
26#include "cx18-streams.h"
27#include "cx18-queue.h"
28#include "cx18-scb.h"
29
1c1e45d1
HV
30void cx18_buf_swap(struct cx18_buffer *buf)
31{
32 int i;
33
34 for (i = 0; i < buf->bytesused; i += 4)
35 swab32s((u32 *)(buf->buf + i));
36}
37
38void cx18_queue_init(struct cx18_queue *q)
39{
40 INIT_LIST_HEAD(&q->list);
b04bce47 41 atomic_set(&q->buffers, 0);
1c1e45d1
HV
42 q->bytesused = 0;
43}
44
66c2a6b0
AW
45struct cx18_queue *_cx18_enqueue(struct cx18_stream *s, struct cx18_buffer *buf,
46 struct cx18_queue *q, int to_front)
1c1e45d1 47{
66c2a6b0
AW
48 /* clear the buffer if it is not to be enqueued to the full queue */
49 if (q != &s->q_full) {
1c1e45d1
HV
50 buf->bytesused = 0;
51 buf->readpos = 0;
52 buf->b_flags = 0;
bca11a57 53 buf->skipped = 0;
1c1e45d1 54 }
66c2a6b0 55
f576ceef 56 mutex_lock(&s->qlock);
66c2a6b0 57
0ef02892
AW
58 /* q_busy is restricted to a max buffer count imposed by firmware */
59 if (q == &s->q_busy &&
60 atomic_read(&q->buffers) >= CX18_MAX_FW_MDLS_PER_STREAM)
66c2a6b0
AW
61 q = &s->q_free;
62
b80e1074
AW
63 if (to_front)
64 list_add(&buf->list, &q->list); /* LIFO */
65 else
66 list_add_tail(&buf->list, &q->list); /* FIFO */
1c1e45d1 67 q->bytesused += buf->bytesused - buf->readpos;
66c2a6b0
AW
68 atomic_inc(&q->buffers);
69
f576ceef 70 mutex_unlock(&s->qlock);
66c2a6b0 71 return q;
1c1e45d1
HV
72}
73
74struct cx18_buffer *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q)
75{
76 struct cx18_buffer *buf = NULL;
1c1e45d1 77
f576ceef 78 mutex_lock(&s->qlock);
1c1e45d1 79 if (!list_empty(&q->list)) {
f6b181ac
AW
80 buf = list_first_entry(&q->list, struct cx18_buffer, list);
81 list_del_init(&buf->list);
1c1e45d1 82 q->bytesused -= buf->bytesused - buf->readpos;
bca11a57 83 buf->skipped = 0;
66c2a6b0 84 atomic_dec(&q->buffers);
1c1e45d1 85 }
f576ceef 86 mutex_unlock(&s->qlock);
1c1e45d1
HV
87 return buf;
88}
89
ee2d64f5 90struct cx18_buffer *cx18_queue_get_buf(struct cx18_stream *s, u32 id,
1c1e45d1
HV
91 u32 bytesused)
92{
93 struct cx18 *cx = s->cx;
bca11a57 94 struct cx18_buffer *buf;
f6b181ac 95 struct cx18_buffer *tmp;
bca11a57 96 struct cx18_buffer *ret = NULL;
1c1e45d1 97
f576ceef 98 mutex_lock(&s->qlock);
f6b181ac 99 list_for_each_entry_safe(buf, tmp, &s->q_busy.list, list) {
ee2d64f5 100 if (buf->id != id) {
bca11a57 101 buf->skipped++;
66c2a6b0 102 if (buf->skipped >= atomic_read(&s->q_busy.buffers)-1) {
bca11a57 103 /* buffer must have fallen out of rotation */
bca11a57
AW
104 CX18_WARN("Skipped %s, buffer %d, %d "
105 "times - it must have dropped out of "
106 "rotation\n", s->name, buf->id,
107 buf->skipped);
66c2a6b0
AW
108 /* move it to q_free */
109 list_move_tail(&buf->list, &s->q_free.list);
110 buf->bytesused = buf->readpos = buf->b_flags =
111 buf->skipped = 0;
112 atomic_dec(&s->q_busy.buffers);
113 atomic_inc(&s->q_free.buffers);
bca11a57 114 }
1c1e45d1 115 continue;
ee2d64f5 116 }
1d6782bd 117
1c1e45d1 118 buf->bytesused = bytesused;
abb096de
AW
119 /* Sync the buffer before we release the qlock */
120 cx18_buf_sync_for_cpu(s, buf);
bca11a57
AW
121 if (s->type == CX18_ENC_STREAM_TYPE_TS) {
122 /*
abb096de
AW
123 * TS doesn't use q_full. As we pull the buffer off of
124 * the queue here, the caller will have to put it back.
bca11a57
AW
125 */
126 list_del_init(&buf->list);
127 } else {
abb096de 128 /* Move buffer from q_busy to q_full */
ee2d64f5 129 list_move_tail(&buf->list, &s->q_full.list);
abb096de 130 set_bit(CX18_F_B_NEED_BUF_SWAP, &buf->b_flags);
66c2a6b0
AW
131 s->q_full.bytesused += buf->bytesused;
132 atomic_inc(&s->q_full.buffers);
ee2d64f5 133 }
66c2a6b0 134 atomic_dec(&s->q_busy.buffers);
ee2d64f5 135
bca11a57
AW
136 ret = buf;
137 break;
1c1e45d1 138 }
66c2a6b0 139 mutex_unlock(&s->qlock);
bca11a57 140 return ret;
1c1e45d1
HV
141}
142
6c9de528
AW
143/* Move all buffers of a queue to q_free, while flushing the buffers */
144static void cx18_queue_flush(struct cx18_stream *s, struct cx18_queue *q)
1c1e45d1 145{
6c9de528 146 struct cx18_buffer *buf;
1c1e45d1 147
6c9de528
AW
148 if (q == &s->q_free)
149 return;
1c1e45d1 150
f576ceef 151 mutex_lock(&s->qlock);
6c9de528 152 while (!list_empty(&q->list)) {
f6b181ac
AW
153 buf = list_first_entry(&q->list, struct cx18_buffer, list);
154 list_move_tail(&buf->list, &s->q_free.list);
bca11a57 155 buf->bytesused = buf->readpos = buf->b_flags = buf->skipped = 0;
b04bce47 156 atomic_inc(&s->q_free.buffers);
1c1e45d1 157 }
6c9de528 158 cx18_queue_init(q);
f576ceef 159 mutex_unlock(&s->qlock);
1c1e45d1
HV
160}
161
162void cx18_flush_queues(struct cx18_stream *s)
163{
66c2a6b0 164 cx18_queue_flush(s, &s->q_busy);
6c9de528 165 cx18_queue_flush(s, &s->q_full);
1c1e45d1
HV
166}
167
168int cx18_stream_alloc(struct cx18_stream *s)
169{
170 struct cx18 *cx = s->cx;
171 int i;
172
173 if (s->buffers == 0)
174 return 0;
175
176 CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers (%dkB total)\n",
177 s->name, s->buffers, s->buf_size,
178 s->buffers * s->buf_size / 1024);
179
c6eb8eaf
HV
180 if (((char __iomem *)&cx->scb->cpu_mdl[cx->mdl_offset + s->buffers] -
181 (char __iomem *)cx->scb) > SCB_RESERVED_SIZE) {
182 unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE -
183 ((char __iomem *)cx->scb->cpu_mdl));
1c1e45d1
HV
184
185 CX18_ERR("Too many buffers, cannot fit in SCB area\n");
186 CX18_ERR("Max buffers = %zd\n",
187 bufsz / sizeof(struct cx18_mdl));
188 return -ENOMEM;
189 }
190
191 s->mdl_offset = cx->mdl_offset;
192
193 /* allocate stream buffers. Initially all buffers are in q_free. */
194 for (i = 0; i < s->buffers; i++) {
3f98387e
HV
195 struct cx18_buffer *buf = kzalloc(sizeof(struct cx18_buffer),
196 GFP_KERNEL|__GFP_NOWARN);
1c1e45d1
HV
197
198 if (buf == NULL)
199 break;
3f98387e 200 buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN);
1c1e45d1
HV
201 if (buf->buf == NULL) {
202 kfree(buf);
203 break;
204 }
205 buf->id = cx->buffer_id++;
206 INIT_LIST_HEAD(&buf->list);
207 buf->dma_handle = pci_map_single(s->cx->dev,
208 buf->buf, s->buf_size, s->dma);
209 cx18_buf_sync_for_cpu(s, buf);
210 cx18_enqueue(s, buf, &s->q_free);
211 }
212 if (i == s->buffers) {
213 cx->mdl_offset += s->buffers;
214 return 0;
215 }
216 CX18_ERR("Couldn't allocate buffers for %s stream\n", s->name);
217 cx18_stream_free(s);
218 return -ENOMEM;
219}
220
221void cx18_stream_free(struct cx18_stream *s)
222{
223 struct cx18_buffer *buf;
224
225 /* move all buffers to q_free */
226 cx18_flush_queues(s);
227
228 /* empty q_free */
229 while ((buf = cx18_dequeue(s, &s->q_free))) {
230 pci_unmap_single(s->cx->dev, buf->dma_handle,
231 s->buf_size, s->dma);
232 kfree(buf->buf);
233 kfree(buf);
234 }
235}