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