Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * dvb_ringbuffer.c: ring buffer implementation for the dvb driver |
| 4 | * |
| 5 | * Copyright (C) 2003 Oliver Endriss |
| 6 | * Copyright (C) 2004 Andrew de Quincey |
| 7 | * |
| 8 | * based on code originally found in av7110.c & dvb_ci.c: |
| 9 | * Copyright (C) 1999-2003 Ralph Metzler |
| 10 | * & Marcus Metzler for convergence integrated media GmbH |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU Lesser General Public License |
| 14 | * as published by the Free Software Foundation; either version 2.1 |
| 15 | * of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU Lesser General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Lesser General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | */ |
| 26 | |
| 27 | |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/errno.h> |
| 30 | #include <linux/kernel.h> |
| 31 | #include <linux/module.h> |
| 32 | #include <linux/sched.h> |
| 33 | #include <linux/string.h> |
| 34 | #include <asm/uaccess.h> |
| 35 | |
| 36 | #include "dvb_ringbuffer.h" |
| 37 | |
| 38 | #define PKT_READY 0 |
| 39 | #define PKT_DISPOSED 1 |
| 40 | |
| 41 | |
| 42 | void dvb_ringbuffer_init(struct dvb_ringbuffer *rbuf, void *data, size_t len) |
| 43 | { |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 44 | rbuf->pread=rbuf->pwrite=0; |
| 45 | rbuf->data=data; |
| 46 | rbuf->size=len; |
Andreas Oberritter | 9d9d6ba | 2006-03-13 13:14:34 -0300 | [diff] [blame] | 47 | rbuf->error=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 49 | init_waitqueue_head(&rbuf->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 51 | spin_lock_init(&(rbuf->lock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | |
| 55 | |
| 56 | int dvb_ringbuffer_empty(struct dvb_ringbuffer *rbuf) |
| 57 | { |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 58 | return (rbuf->pread==rbuf->pwrite); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | |
| 62 | |
| 63 | ssize_t dvb_ringbuffer_free(struct dvb_ringbuffer *rbuf) |
| 64 | { |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 65 | ssize_t free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 67 | free = rbuf->pread - rbuf->pwrite; |
| 68 | if (free <= 0) |
| 69 | free += rbuf->size; |
| 70 | return free-1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | |
| 74 | |
| 75 | ssize_t dvb_ringbuffer_avail(struct dvb_ringbuffer *rbuf) |
| 76 | { |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 77 | ssize_t avail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 79 | avail = rbuf->pwrite - rbuf->pread; |
| 80 | if (avail < 0) |
| 81 | avail += rbuf->size; |
| 82 | return avail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
| 86 | |
| 87 | void dvb_ringbuffer_flush(struct dvb_ringbuffer *rbuf) |
| 88 | { |
Andreas Oberritter | 4304954 | 2006-03-14 17:30:09 -0300 | [diff] [blame] | 89 | rbuf->pread = rbuf->pwrite; |
Andreas Oberritter | 9d9d6ba | 2006-03-13 13:14:34 -0300 | [diff] [blame] | 90 | rbuf->error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | |
| 94 | |
| 95 | void dvb_ringbuffer_flush_spinlock_wakeup(struct dvb_ringbuffer *rbuf) |
| 96 | { |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 97 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 99 | spin_lock_irqsave(&rbuf->lock, flags); |
| 100 | dvb_ringbuffer_flush(rbuf); |
| 101 | spin_unlock_irqrestore(&rbuf->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 103 | wake_up(&rbuf->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | |
| 107 | |
| 108 | ssize_t dvb_ringbuffer_read(struct dvb_ringbuffer *rbuf, u8 *buf, size_t len, int usermem) |
| 109 | { |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 110 | size_t todo = len; |
| 111 | size_t split; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 113 | split = (rbuf->pread + len > rbuf->size) ? rbuf->size - rbuf->pread : 0; |
| 114 | if (split > 0) { |
| 115 | if (!usermem) |
Michael Krufky | 50c25ff | 2006-01-09 15:25:34 -0200 | [diff] [blame] | 116 | memcpy(buf, rbuf->data+rbuf->pread, split); |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 117 | else |
Michael Krufky | 50c25ff | 2006-01-09 15:25:34 -0200 | [diff] [blame] | 118 | if (copy_to_user(buf, rbuf->data+rbuf->pread, split)) |
| 119 | return -EFAULT; |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 120 | buf += split; |
| 121 | todo -= split; |
| 122 | rbuf->pread = 0; |
| 123 | } |
| 124 | if (!usermem) |
| 125 | memcpy(buf, rbuf->data+rbuf->pread, todo); |
| 126 | else |
| 127 | if (copy_to_user(buf, rbuf->data+rbuf->pread, todo)) |
Michael Krufky | 50c25ff | 2006-01-09 15:25:34 -0200 | [diff] [blame] | 128 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 130 | rbuf->pread = (rbuf->pread + todo) % rbuf->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 132 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | |
| 136 | |
| 137 | ssize_t dvb_ringbuffer_write(struct dvb_ringbuffer *rbuf, const u8 *buf, size_t len) |
| 138 | { |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 139 | size_t todo = len; |
| 140 | size_t split; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 142 | split = (rbuf->pwrite + len > rbuf->size) ? rbuf->size - rbuf->pwrite : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 144 | if (split > 0) { |
| 145 | memcpy(rbuf->data+rbuf->pwrite, buf, split); |
| 146 | buf += split; |
| 147 | todo -= split; |
| 148 | rbuf->pwrite = 0; |
| 149 | } |
| 150 | memcpy(rbuf->data+rbuf->pwrite, buf, todo); |
| 151 | rbuf->pwrite = (rbuf->pwrite + todo) % rbuf->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 153 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | ssize_t dvb_ringbuffer_pkt_write(struct dvb_ringbuffer *rbuf, u8* buf, size_t len) |
| 157 | { |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 158 | int status; |
| 159 | ssize_t oldpwrite = rbuf->pwrite; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 161 | DVB_RINGBUFFER_WRITE_BYTE(rbuf, len >> 8); |
| 162 | DVB_RINGBUFFER_WRITE_BYTE(rbuf, len & 0xff); |
| 163 | DVB_RINGBUFFER_WRITE_BYTE(rbuf, PKT_READY); |
| 164 | status = dvb_ringbuffer_write(rbuf, buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 166 | if (status < 0) rbuf->pwrite = oldpwrite; |
| 167 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | ssize_t dvb_ringbuffer_pkt_read(struct dvb_ringbuffer *rbuf, size_t idx, |
Michael Krufky | 50c25ff | 2006-01-09 15:25:34 -0200 | [diff] [blame] | 171 | int offset, u8* buf, size_t len, int usermem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 173 | size_t todo; |
| 174 | size_t split; |
| 175 | size_t pktlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 177 | pktlen = rbuf->data[idx] << 8; |
| 178 | pktlen |= rbuf->data[(idx + 1) % rbuf->size]; |
| 179 | if (offset > pktlen) return -EINVAL; |
| 180 | if ((offset + len) > pktlen) len = pktlen - offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 182 | idx = (idx + DVB_RINGBUFFER_PKTHDRSIZE + offset) % rbuf->size; |
| 183 | todo = len; |
| 184 | split = ((idx + len) > rbuf->size) ? rbuf->size - idx : 0; |
| 185 | if (split > 0) { |
| 186 | if (!usermem) |
Michael Krufky | 50c25ff | 2006-01-09 15:25:34 -0200 | [diff] [blame] | 187 | memcpy(buf, rbuf->data+idx, split); |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 188 | else |
Michael Krufky | 50c25ff | 2006-01-09 15:25:34 -0200 | [diff] [blame] | 189 | if (copy_to_user(buf, rbuf->data+idx, split)) |
| 190 | return -EFAULT; |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 191 | buf += split; |
| 192 | todo -= split; |
| 193 | idx = 0; |
| 194 | } |
| 195 | if (!usermem) |
| 196 | memcpy(buf, rbuf->data+idx, todo); |
| 197 | else |
| 198 | if (copy_to_user(buf, rbuf->data+idx, todo)) |
Michael Krufky | 50c25ff | 2006-01-09 15:25:34 -0200 | [diff] [blame] | 199 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 201 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void dvb_ringbuffer_pkt_dispose(struct dvb_ringbuffer *rbuf, size_t idx) |
| 205 | { |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 206 | size_t pktlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 208 | rbuf->data[(idx + 2) % rbuf->size] = PKT_DISPOSED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 210 | // clean up disposed packets |
| 211 | while(dvb_ringbuffer_avail(rbuf) > DVB_RINGBUFFER_PKTHDRSIZE) { |
| 212 | if (DVB_RINGBUFFER_PEEK(rbuf, 2) == PKT_DISPOSED) { |
Michael Krufky | 50c25ff | 2006-01-09 15:25:34 -0200 | [diff] [blame] | 213 | pktlen = DVB_RINGBUFFER_PEEK(rbuf, 0) << 8; |
| 214 | pktlen |= DVB_RINGBUFFER_PEEK(rbuf, 1); |
| 215 | DVB_RINGBUFFER_SKIP(rbuf, pktlen + DVB_RINGBUFFER_PKTHDRSIZE); |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 216 | } else { |
Michael Krufky | 50c25ff | 2006-01-09 15:25:34 -0200 | [diff] [blame] | 217 | // first packet is not disposed, so we stop cleaning now |
| 218 | break; |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 219 | } |
| 220 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | ssize_t dvb_ringbuffer_pkt_next(struct dvb_ringbuffer *rbuf, size_t idx, size_t* pktlen) |
| 224 | { |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 225 | int consumed; |
| 226 | int curpktlen; |
| 227 | int curpktstatus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 229 | if (idx == -1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | idx = rbuf->pread; |
| 231 | } else { |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 232 | curpktlen = rbuf->data[idx] << 8; |
| 233 | curpktlen |= rbuf->data[(idx + 1) % rbuf->size]; |
| 234 | idx = (idx + curpktlen + DVB_RINGBUFFER_PKTHDRSIZE) % rbuf->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 237 | consumed = (idx - rbuf->pread) % rbuf->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 239 | while((dvb_ringbuffer_avail(rbuf) - consumed) > DVB_RINGBUFFER_PKTHDRSIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 241 | curpktlen = rbuf->data[idx] << 8; |
| 242 | curpktlen |= rbuf->data[(idx + 1) % rbuf->size]; |
| 243 | curpktstatus = rbuf->data[(idx + 2) % rbuf->size]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 245 | if (curpktstatus == PKT_READY) { |
Michael Krufky | 50c25ff | 2006-01-09 15:25:34 -0200 | [diff] [blame] | 246 | *pktlen = curpktlen; |
| 247 | return idx; |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 248 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 250 | consumed += curpktlen + DVB_RINGBUFFER_PKTHDRSIZE; |
| 251 | idx = (idx + curpktlen + DVB_RINGBUFFER_PKTHDRSIZE) % rbuf->size; |
| 252 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 254 | // no packets available |
| 255 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | |
| 259 | |
| 260 | EXPORT_SYMBOL(dvb_ringbuffer_init); |
| 261 | EXPORT_SYMBOL(dvb_ringbuffer_empty); |
| 262 | EXPORT_SYMBOL(dvb_ringbuffer_free); |
| 263 | EXPORT_SYMBOL(dvb_ringbuffer_avail); |
| 264 | EXPORT_SYMBOL(dvb_ringbuffer_flush); |
| 265 | EXPORT_SYMBOL(dvb_ringbuffer_flush_spinlock_wakeup); |
| 266 | EXPORT_SYMBOL(dvb_ringbuffer_read); |
| 267 | EXPORT_SYMBOL(dvb_ringbuffer_write); |
| 268 | EXPORT_SYMBOL(dvb_ringbuffer_pkt_write); |
| 269 | EXPORT_SYMBOL(dvb_ringbuffer_pkt_read); |
| 270 | EXPORT_SYMBOL(dvb_ringbuffer_pkt_dispose); |
| 271 | EXPORT_SYMBOL(dvb_ringbuffer_pkt_next); |