V4L/DVB (9720): cx18: Major rewrite of interrupt handling for incoming mailbox processing
[safe/jmp/linux-2.6] / drivers / media / video / cx18 / cx18-queue.c
1 /*
2  *  cx18 buffer queues
3  *
4  *  Derived from ivtv-queue.c
5  *
6  *  Copyright (C) 2007  Hans Verkuil <hverkuil@xs4all.nl>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21  *  02111-1307  USA
22  */
23
24 #include "cx18-driver.h"
25 #include "cx18-streams.h"
26 #include "cx18-queue.h"
27 #include "cx18-scb.h"
28
29 void cx18_buf_swap(struct cx18_buffer *buf)
30 {
31         int i;
32
33         for (i = 0; i < buf->bytesused; i += 4)
34                 swab32s((u32 *)(buf->buf + i));
35 }
36
37 void cx18_queue_init(struct cx18_queue *q)
38 {
39         INIT_LIST_HEAD(&q->list);
40         atomic_set(&q->buffers, 0);
41         q->bytesused = 0;
42 }
43
44 void cx18_enqueue(struct cx18_stream *s, struct cx18_buffer *buf,
45                 struct cx18_queue *q)
46 {
47         unsigned long flags = 0;
48
49         /* clear the buffer if it is going to be enqueued to the free queue */
50         if (q == &s->q_free) {
51                 buf->bytesused = 0;
52                 buf->readpos = 0;
53                 buf->b_flags = 0;
54         }
55         spin_lock_irqsave(&s->qlock, flags);
56         list_add_tail(&buf->list, &q->list);
57         atomic_inc(&q->buffers);
58         q->bytesused += buf->bytesused - buf->readpos;
59         spin_unlock_irqrestore(&s->qlock, flags);
60 }
61
62 struct cx18_buffer *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q)
63 {
64         struct cx18_buffer *buf = NULL;
65         unsigned long flags = 0;
66
67         spin_lock_irqsave(&s->qlock, flags);
68         if (!list_empty(&q->list)) {
69                 buf = list_entry(q->list.next, struct cx18_buffer, list);
70                 list_del_init(q->list.next);
71                 atomic_dec(&q->buffers);
72                 q->bytesused -= buf->bytesused - buf->readpos;
73         }
74         spin_unlock_irqrestore(&s->qlock, flags);
75         return buf;
76 }
77
78 struct cx18_buffer *cx18_queue_get_buf(struct cx18_stream *s, u32 id,
79         u32 bytesused)
80 {
81         struct cx18 *cx = s->cx;
82         struct list_head *p;
83         unsigned long flags = 0;
84
85         spin_lock_irqsave(&s->qlock, flags);
86         list_for_each(p, &s->q_free.list) {
87                 struct cx18_buffer *buf =
88                         list_entry(p, struct cx18_buffer, list);
89
90                 if (buf->id != id) {
91                         CX18_DEBUG_HI_DMA("Skipping buffer %d searching for %d "
92                                           "in stream %s q_free\n", buf->id, id,
93                                           s->name);
94                         continue;
95                 }
96
97                 buf->bytesused = bytesused;
98                 if (s->type != CX18_ENC_STREAM_TYPE_TS) {
99                         atomic_dec(&s->q_free.buffers);
100                         atomic_inc(&s->q_full.buffers);
101                         s->q_full.bytesused += buf->bytesused;
102                         list_move_tail(&buf->list, &s->q_full.list);
103                 }
104
105                 spin_unlock_irqrestore(&s->qlock, flags);
106                 return buf;
107         }
108         spin_unlock_irqrestore(&s->qlock, flags);
109         CX18_ERR("Cannot find buffer %d for stream %s\n", id, s->name);
110         return NULL;
111 }
112
113 /* Move all buffers of a queue to q_free, while flushing the buffers */
114 static void cx18_queue_flush(struct cx18_stream *s, struct cx18_queue *q)
115 {
116         unsigned long flags;
117         struct cx18_buffer *buf;
118
119         if (q == &s->q_free)
120                 return;
121
122         spin_lock_irqsave(&s->qlock, flags);
123         while (!list_empty(&q->list)) {
124                 buf = list_entry(q->list.next, struct cx18_buffer, list);
125                 list_move_tail(q->list.next, &s->q_free.list);
126                 buf->bytesused = buf->readpos = buf->b_flags = 0;
127                 atomic_inc(&s->q_free.buffers);
128         }
129         cx18_queue_init(q);
130         spin_unlock_irqrestore(&s->qlock, flags);
131 }
132
133 void cx18_flush_queues(struct cx18_stream *s)
134 {
135         cx18_queue_flush(s, &s->q_io);
136         cx18_queue_flush(s, &s->q_full);
137 }
138
139 int cx18_stream_alloc(struct cx18_stream *s)
140 {
141         struct cx18 *cx = s->cx;
142         int i;
143
144         if (s->buffers == 0)
145                 return 0;
146
147         CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers (%dkB total)\n",
148                 s->name, s->buffers, s->buf_size,
149                 s->buffers * s->buf_size / 1024);
150
151         if (((char __iomem *)&cx->scb->cpu_mdl[cx->mdl_offset + s->buffers] -
152                                 (char __iomem *)cx->scb) > SCB_RESERVED_SIZE) {
153                 unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE -
154                                         ((char __iomem *)cx->scb->cpu_mdl));
155
156                 CX18_ERR("Too many buffers, cannot fit in SCB area\n");
157                 CX18_ERR("Max buffers = %zd\n",
158                         bufsz / sizeof(struct cx18_mdl));
159                 return -ENOMEM;
160         }
161
162         s->mdl_offset = cx->mdl_offset;
163
164         /* allocate stream buffers. Initially all buffers are in q_free. */
165         for (i = 0; i < s->buffers; i++) {
166                 struct cx18_buffer *buf = kzalloc(sizeof(struct cx18_buffer),
167                                                 GFP_KERNEL|__GFP_NOWARN);
168
169                 if (buf == NULL)
170                         break;
171                 buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN);
172                 if (buf->buf == NULL) {
173                         kfree(buf);
174                         break;
175                 }
176                 buf->id = cx->buffer_id++;
177                 INIT_LIST_HEAD(&buf->list);
178                 buf->dma_handle = pci_map_single(s->cx->dev,
179                                 buf->buf, s->buf_size, s->dma);
180                 cx18_buf_sync_for_cpu(s, buf);
181                 cx18_enqueue(s, buf, &s->q_free);
182         }
183         if (i == s->buffers) {
184                 cx->mdl_offset += s->buffers;
185                 return 0;
186         }
187         CX18_ERR("Couldn't allocate buffers for %s stream\n", s->name);
188         cx18_stream_free(s);
189         return -ENOMEM;
190 }
191
192 void cx18_stream_free(struct cx18_stream *s)
193 {
194         struct cx18_buffer *buf;
195
196         /* move all buffers to q_free */
197         cx18_flush_queues(s);
198
199         /* empty q_free */
200         while ((buf = cx18_dequeue(s, &s->q_free))) {
201                 pci_unmap_single(s->cx->dev, buf->dma_handle,
202                                 s->buf_size, s->dma);
203                 kfree(buf->buf);
204                 kfree(buf);
205         }
206 }