aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/cx18/cx18-queue.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/video/cx18/cx18-queue.c')
-rw-r--r--drivers/media/video/cx18/cx18-queue.c129
1 files changed, 32 insertions, 97 deletions
diff --git a/drivers/media/video/cx18/cx18-queue.c b/drivers/media/video/cx18/cx18-queue.c
index 6990b77c6200..dbe792ac3001 100644
--- a/drivers/media/video/cx18/cx18-queue.c
+++ b/drivers/media/video/cx18/cx18-queue.c
@@ -78,12 +78,13 @@ struct cx18_buffer *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q)
78 return buf; 78 return buf;
79} 79}
80 80
81struct cx18_buffer *cx18_queue_find_buf(struct cx18_stream *s, u32 id, 81struct cx18_buffer *cx18_queue_get_buf_irq(struct cx18_stream *s, u32 id,
82 u32 bytesused) 82 u32 bytesused)
83{ 83{
84 struct cx18 *cx = s->cx; 84 struct cx18 *cx = s->cx;
85 struct list_head *p; 85 struct list_head *p;
86 86
87 spin_lock(&s->qlock);
87 list_for_each(p, &s->q_free.list) { 88 list_for_each(p, &s->q_free.list) {
88 struct cx18_buffer *buf = 89 struct cx18_buffer *buf =
89 list_entry(p, struct cx18_buffer, list); 90 list_entry(p, struct cx18_buffer, list);
@@ -92,114 +93,48 @@ struct cx18_buffer *cx18_queue_find_buf(struct cx18_stream *s, u32 id,
92 continue; 93 continue;
93 buf->bytesused = bytesused; 94 buf->bytesused = bytesused;
94 /* the transport buffers are handled differently, 95 /* the transport buffers are handled differently,
95 so there is no need to move them to the full queue */ 96 they are not moved to the full queue */
96 if (s->type == CX18_ENC_STREAM_TYPE_TS) 97 if (s->type != CX18_ENC_STREAM_TYPE_TS) {
97 return buf; 98 s->q_free.buffers--;
98 s->q_free.buffers--; 99 s->q_free.length -= s->buf_size;
99 s->q_free.length -= s->buf_size; 100 s->q_full.buffers++;
100 s->q_full.buffers++; 101 s->q_full.length += s->buf_size;
101 s->q_full.length += s->buf_size; 102 s->q_full.bytesused += buf->bytesused;
102 s->q_full.bytesused += buf->bytesused; 103 list_move_tail(&buf->list, &s->q_full.list);
103 list_move_tail(&buf->list, &s->q_full.list); 104 }
105 spin_unlock(&s->qlock);
104 return buf; 106 return buf;
105 } 107 }
108 spin_unlock(&s->qlock);
106 CX18_ERR("Cannot find buffer %d for stream %s\n", id, s->name); 109 CX18_ERR("Cannot find buffer %d for stream %s\n", id, s->name);
107 return NULL; 110 return NULL;
108} 111}
109 112
110static void cx18_queue_move_buf(struct cx18_stream *s, struct cx18_queue *from, 113/* Move all buffers of a queue to q_free, while flushing the buffers */
111 struct cx18_queue *to, int clear, int full) 114static void cx18_queue_flush(struct cx18_stream *s, struct cx18_queue *q)
112{
113 struct cx18_buffer *buf =
114 list_entry(from->list.next, struct cx18_buffer, list);
115
116 list_move_tail(from->list.next, &to->list);
117 from->buffers--;
118 from->length -= s->buf_size;
119 from->bytesused -= buf->bytesused - buf->readpos;
120 /* special handling for q_free */
121 if (clear)
122 buf->bytesused = buf->readpos = buf->b_flags = 0;
123 else if (full) {
124 /* special handling for stolen buffers, assume
125 all bytes are used. */
126 buf->bytesused = s->buf_size;
127 buf->readpos = buf->b_flags = 0;
128 }
129 to->buffers++;
130 to->length += s->buf_size;
131 to->bytesused += buf->bytesused - buf->readpos;
132}
133
134/* Move 'needed_bytes' worth of buffers from queue 'from' into queue 'to'.
135 If 'needed_bytes' == 0, then move all buffers from 'from' into 'to'.
136 If 'steal' != NULL, then buffers may also taken from that queue if
137 needed.
138
139 The buffer is automatically cleared if it goes to the free queue. It is
140 also cleared if buffers need to be taken from the 'steal' queue and
141 the 'from' queue is the free queue.
142
143 When 'from' is q_free, then needed_bytes is compared to the total
144 available buffer length, otherwise needed_bytes is compared to the
145 bytesused value. For the 'steal' queue the total available buffer
146 length is always used.
147
148 -ENOMEM is returned if the buffers could not be obtained, 0 if all
149 buffers where obtained from the 'from' list and if non-zero then
150 the number of stolen buffers is returned. */
151static int cx18_queue_move(struct cx18_stream *s, struct cx18_queue *from,
152 struct cx18_queue *steal, struct cx18_queue *to,
153 int needed_bytes)
154{ 115{
155 unsigned long flags; 116 unsigned long flags;
156 int rc = 0; 117 struct cx18_buffer *buf;
157 int from_free = from == &s->q_free;
158 int to_free = to == &s->q_free;
159 int bytes_available;
160
161 spin_lock_irqsave(&s->qlock, flags);
162 if (needed_bytes == 0) {
163 from_free = 1;
164 needed_bytes = from->length;
165 }
166
167 bytes_available = from_free ? from->length : from->bytesused;
168 bytes_available += steal ? steal->length : 0;
169 118
170 if (bytes_available < needed_bytes) { 119 if (q == &s->q_free)
171 spin_unlock_irqrestore(&s->qlock, flags); 120 return;
172 return -ENOMEM;
173 }
174 if (from_free) {
175 u32 old_length = to->length;
176 121
177 while (to->length - old_length < needed_bytes) { 122 spin_lock_irqsave(&s->qlock, flags);
178 if (list_empty(&from->list)) 123 while (!list_empty(&q->list)) {
179 from = steal; 124 buf = list_entry(q->list.next, struct cx18_buffer, list);
180 if (from == steal) 125 list_move_tail(q->list.next, &s->q_free.list);
181 rc++; /* keep track of 'stolen' buffers */ 126 buf->bytesused = buf->readpos = buf->b_flags = 0;
182 cx18_queue_move_buf(s, from, to, 1, 0); 127 s->q_free.buffers++;
183 } 128 s->q_free.length += s->buf_size;
184 } else {
185 u32 old_bytesused = to->bytesused;
186
187 while (to->bytesused - old_bytesused < needed_bytes) {
188 if (list_empty(&from->list))
189 from = steal;
190 if (from == steal)
191 rc++; /* keep track of 'stolen' buffers */
192 cx18_queue_move_buf(s, from, to, to_free, rc);
193 }
194 } 129 }
130 cx18_queue_init(q);
195 spin_unlock_irqrestore(&s->qlock, flags); 131 spin_unlock_irqrestore(&s->qlock, flags);
196 return rc;
197} 132}
198 133
199void cx18_flush_queues(struct cx18_stream *s) 134void cx18_flush_queues(struct cx18_stream *s)
200{ 135{
201 cx18_queue_move(s, &s->q_io, NULL, &s->q_free, 0); 136 cx18_queue_flush(s, &s->q_io);
202 cx18_queue_move(s, &s->q_full, NULL, &s->q_free, 0); 137 cx18_queue_flush(s, &s->q_full);
203} 138}
204 139
205int cx18_stream_alloc(struct cx18_stream *s) 140int cx18_stream_alloc(struct cx18_stream *s)
@@ -214,10 +149,10 @@ int cx18_stream_alloc(struct cx18_stream *s)
214 s->name, s->buffers, s->buf_size, 149 s->name, s->buffers, s->buf_size,
215 s->buffers * s->buf_size / 1024); 150 s->buffers * s->buf_size / 1024);
216 151
217 if (((char *)&cx->scb->cpu_mdl[cx->mdl_offset + s->buffers] - 152 if (((char __iomem *)&cx->scb->cpu_mdl[cx->mdl_offset + s->buffers] -
218 (char *)cx->scb) > SCB_RESERVED_SIZE) { 153 (char __iomem *)cx->scb) > SCB_RESERVED_SIZE) {
219 unsigned bufsz = (((char *)cx->scb) + SCB_RESERVED_SIZE - 154 unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE -
220 ((char *)cx->scb->cpu_mdl)); 155 ((char __iomem *)cx->scb->cpu_mdl));
221 156
222 CX18_ERR("Too many buffers, cannot fit in SCB area\n"); 157 CX18_ERR("Too many buffers, cannot fit in SCB area\n");
223 CX18_ERR("Max buffers = %zd\n", 158 CX18_ERR("Max buffers = %zd\n",