Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 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 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 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); |
Andy Walls | b04bce4 | 2008-08-22 21:03:11 -0300 | [diff] [blame] | 40 | atomic_set(&q->buffers, 0); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 41 | q->bytesused = 0; |
| 42 | } |
| 43 | |
| 44 | void cx18_enqueue(struct cx18_stream *s, struct cx18_buffer *buf, |
| 45 | struct cx18_queue *q) |
| 46 | { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 47 | /* clear the buffer if it is going to be enqueued to the free queue */ |
| 48 | if (q == &s->q_free) { |
| 49 | buf->bytesused = 0; |
| 50 | buf->readpos = 0; |
| 51 | buf->b_flags = 0; |
| 52 | } |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 53 | mutex_lock(&s->qlock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 54 | list_add_tail(&buf->list, &q->list); |
Andy Walls | b04bce4 | 2008-08-22 21:03:11 -0300 | [diff] [blame] | 55 | atomic_inc(&q->buffers); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 56 | q->bytesused += buf->bytesused - buf->readpos; |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 57 | mutex_unlock(&s->qlock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | struct cx18_buffer *cx18_dequeue(struct cx18_stream *s, struct cx18_queue *q) |
| 61 | { |
| 62 | struct cx18_buffer *buf = NULL; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 63 | |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 64 | mutex_lock(&s->qlock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 65 | if (!list_empty(&q->list)) { |
| 66 | buf = list_entry(q->list.next, struct cx18_buffer, list); |
| 67 | list_del_init(q->list.next); |
Andy Walls | b04bce4 | 2008-08-22 21:03:11 -0300 | [diff] [blame] | 68 | atomic_dec(&q->buffers); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 69 | q->bytesused -= buf->bytesused - buf->readpos; |
| 70 | } |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 71 | mutex_unlock(&s->qlock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 72 | return buf; |
| 73 | } |
| 74 | |
Andy Walls | ee2d64f | 2008-11-16 01:38:19 -0300 | [diff] [blame] | 75 | struct cx18_buffer *cx18_queue_get_buf(struct cx18_stream *s, u32 id, |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 76 | u32 bytesused) |
| 77 | { |
| 78 | struct cx18 *cx = s->cx; |
| 79 | struct list_head *p; |
| 80 | |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 81 | mutex_lock(&s->qlock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 82 | list_for_each(p, &s->q_free.list) { |
| 83 | struct cx18_buffer *buf = |
| 84 | list_entry(p, struct cx18_buffer, list); |
| 85 | |
Andy Walls | ee2d64f | 2008-11-16 01:38:19 -0300 | [diff] [blame] | 86 | if (buf->id != id) { |
| 87 | CX18_DEBUG_HI_DMA("Skipping buffer %d searching for %d " |
| 88 | "in stream %s q_free\n", buf->id, id, |
| 89 | s->name); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 90 | continue; |
Andy Walls | ee2d64f | 2008-11-16 01:38:19 -0300 | [diff] [blame] | 91 | } |
Andy Walls | 1d6782b | 2008-11-05 00:49:14 -0300 | [diff] [blame] | 92 | |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 93 | buf->bytesused = bytesused; |
Andy Walls | ee2d64f | 2008-11-16 01:38:19 -0300 | [diff] [blame] | 94 | if (s->type != CX18_ENC_STREAM_TYPE_TS) { |
| 95 | atomic_dec(&s->q_free.buffers); |
| 96 | atomic_inc(&s->q_full.buffers); |
| 97 | s->q_full.bytesused += buf->bytesused; |
| 98 | list_move_tail(&buf->list, &s->q_full.list); |
| 99 | } |
Andy Walls | 1d6782b | 2008-11-05 00:49:14 -0300 | [diff] [blame] | 100 | |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 101 | mutex_unlock(&s->qlock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 102 | return buf; |
| 103 | } |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 104 | mutex_unlock(&s->qlock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 105 | CX18_ERR("Cannot find buffer %d for stream %s\n", id, s->name); |
| 106 | return NULL; |
| 107 | } |
| 108 | |
Andy Walls | 6c9de52 | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 109 | /* Move all buffers of a queue to q_free, while flushing the buffers */ |
| 110 | static void cx18_queue_flush(struct cx18_stream *s, struct cx18_queue *q) |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 111 | { |
Andy Walls | 6c9de52 | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 112 | struct cx18_buffer *buf; |
| 113 | |
| 114 | if (q == &s->q_free) |
| 115 | return; |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 116 | |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 117 | mutex_lock(&s->qlock); |
Andy Walls | 6c9de52 | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 118 | while (!list_empty(&q->list)) { |
| 119 | buf = list_entry(q->list.next, struct cx18_buffer, list); |
| 120 | list_move_tail(q->list.next, &s->q_free.list); |
| 121 | buf->bytesused = buf->readpos = buf->b_flags = 0; |
Andy Walls | b04bce4 | 2008-08-22 21:03:11 -0300 | [diff] [blame] | 122 | atomic_inc(&s->q_free.buffers); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 123 | } |
Andy Walls | 6c9de52 | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 124 | cx18_queue_init(q); |
Andy Walls | f576cee | 2008-11-16 20:18:05 -0300 | [diff] [blame] | 125 | mutex_unlock(&s->qlock); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void cx18_flush_queues(struct cx18_stream *s) |
| 129 | { |
Andy Walls | 6c9de52 | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 130 | cx18_queue_flush(s, &s->q_io); |
| 131 | cx18_queue_flush(s, &s->q_full); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | int cx18_stream_alloc(struct cx18_stream *s) |
| 135 | { |
| 136 | struct cx18 *cx = s->cx; |
| 137 | int i; |
| 138 | |
| 139 | if (s->buffers == 0) |
| 140 | return 0; |
| 141 | |
| 142 | CX18_DEBUG_INFO("Allocate %s stream: %d x %d buffers (%dkB total)\n", |
| 143 | s->name, s->buffers, s->buf_size, |
| 144 | s->buffers * s->buf_size / 1024); |
| 145 | |
Hans Verkuil | c6eb8ea | 2008-09-03 17:11:54 -0300 | [diff] [blame] | 146 | if (((char __iomem *)&cx->scb->cpu_mdl[cx->mdl_offset + s->buffers] - |
| 147 | (char __iomem *)cx->scb) > SCB_RESERVED_SIZE) { |
| 148 | unsigned bufsz = (((char __iomem *)cx->scb) + SCB_RESERVED_SIZE - |
| 149 | ((char __iomem *)cx->scb->cpu_mdl)); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 150 | |
| 151 | CX18_ERR("Too many buffers, cannot fit in SCB area\n"); |
| 152 | CX18_ERR("Max buffers = %zd\n", |
| 153 | bufsz / sizeof(struct cx18_mdl)); |
| 154 | return -ENOMEM; |
| 155 | } |
| 156 | |
| 157 | s->mdl_offset = cx->mdl_offset; |
| 158 | |
| 159 | /* allocate stream buffers. Initially all buffers are in q_free. */ |
| 160 | for (i = 0; i < s->buffers; i++) { |
Hans Verkuil | 3f98387 | 2008-05-01 10:31:12 -0300 | [diff] [blame] | 161 | struct cx18_buffer *buf = kzalloc(sizeof(struct cx18_buffer), |
| 162 | GFP_KERNEL|__GFP_NOWARN); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 163 | |
| 164 | if (buf == NULL) |
| 165 | break; |
Hans Verkuil | 3f98387 | 2008-05-01 10:31:12 -0300 | [diff] [blame] | 166 | buf->buf = kmalloc(s->buf_size, GFP_KERNEL|__GFP_NOWARN); |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 167 | if (buf->buf == NULL) { |
| 168 | kfree(buf); |
| 169 | break; |
| 170 | } |
| 171 | buf->id = cx->buffer_id++; |
| 172 | INIT_LIST_HEAD(&buf->list); |
| 173 | buf->dma_handle = pci_map_single(s->cx->dev, |
| 174 | buf->buf, s->buf_size, s->dma); |
| 175 | cx18_buf_sync_for_cpu(s, buf); |
| 176 | cx18_enqueue(s, buf, &s->q_free); |
| 177 | } |
| 178 | if (i == s->buffers) { |
| 179 | cx->mdl_offset += s->buffers; |
| 180 | return 0; |
| 181 | } |
| 182 | CX18_ERR("Couldn't allocate buffers for %s stream\n", s->name); |
| 183 | cx18_stream_free(s); |
| 184 | return -ENOMEM; |
| 185 | } |
| 186 | |
| 187 | void cx18_stream_free(struct cx18_stream *s) |
| 188 | { |
| 189 | struct cx18_buffer *buf; |
| 190 | |
| 191 | /* move all buffers to q_free */ |
| 192 | cx18_flush_queues(s); |
| 193 | |
| 194 | /* empty q_free */ |
| 195 | while ((buf = cx18_dequeue(s, &s->q_free))) { |
Hans Verkuil | 1c1e45d | 2008-04-28 20:24:33 -0300 | [diff] [blame] | 196 | pci_unmap_single(s->cx->dev, buf->dma_handle, |
| 197 | s->buf_size, s->dma); |
| 198 | kfree(buf->buf); |
| 199 | kfree(buf); |
| 200 | } |
| 201 | } |