3 * dvb_ringbuffer.c: ring buffer implementation for the dvb driver
5 * Copyright (C) 2003 Oliver Endriss
6 * Copyright (C) 2004 Andrew de Quincey
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
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.
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.
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.
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>
36 #include "dvb_ringbuffer.h"
39 #define PKT_DISPOSED 1
42 void dvb_ringbuffer_init(struct dvb_ringbuffer
*rbuf
, void *data
, size_t len
)
44 rbuf
->pread
=rbuf
->pwrite
=0;
49 init_waitqueue_head(&rbuf
->queue
);
51 spin_lock_init(&(rbuf
->lock
));
56 int dvb_ringbuffer_empty(struct dvb_ringbuffer
*rbuf
)
58 return (rbuf
->pread
==rbuf
->pwrite
);
63 ssize_t
dvb_ringbuffer_free(struct dvb_ringbuffer
*rbuf
)
67 free
= rbuf
->pread
- rbuf
->pwrite
;
75 ssize_t
dvb_ringbuffer_avail(struct dvb_ringbuffer
*rbuf
)
79 avail
= rbuf
->pwrite
- rbuf
->pread
;
87 void dvb_ringbuffer_flush(struct dvb_ringbuffer
*rbuf
)
89 rbuf
->pread
= rbuf
->pwrite
;
92 EXPORT_SYMBOL(dvb_ringbuffer_flush
);
94 void dvb_ringbuffer_reset(struct dvb_ringbuffer
*rbuf
)
96 rbuf
->pread
= rbuf
->pwrite
= 0;
100 void dvb_ringbuffer_flush_spinlock_wakeup(struct dvb_ringbuffer
*rbuf
)
104 spin_lock_irqsave(&rbuf
->lock
, flags
);
105 dvb_ringbuffer_flush(rbuf
);
106 spin_unlock_irqrestore(&rbuf
->lock
, flags
);
108 wake_up(&rbuf
->queue
);
111 ssize_t
dvb_ringbuffer_read_user(struct dvb_ringbuffer
*rbuf
, u8 __user
*buf
, size_t len
)
116 split
= (rbuf
->pread
+ len
> rbuf
->size
) ? rbuf
->size
- rbuf
->pread
: 0;
118 if (copy_to_user(buf
, rbuf
->data
+rbuf
->pread
, split
))
124 if (copy_to_user(buf
, rbuf
->data
+rbuf
->pread
, todo
))
127 rbuf
->pread
= (rbuf
->pread
+ todo
) % rbuf
->size
;
132 void dvb_ringbuffer_read(struct dvb_ringbuffer
*rbuf
, u8
*buf
, size_t len
)
137 split
= (rbuf
->pread
+ len
> rbuf
->size
) ? rbuf
->size
- rbuf
->pread
: 0;
139 memcpy(buf
, rbuf
->data
+rbuf
->pread
, split
);
144 memcpy(buf
, rbuf
->data
+rbuf
->pread
, todo
);
146 rbuf
->pread
= (rbuf
->pread
+ todo
) % rbuf
->size
;
150 ssize_t
dvb_ringbuffer_write(struct dvb_ringbuffer
*rbuf
, const u8
*buf
, size_t len
)
155 split
= (rbuf
->pwrite
+ len
> rbuf
->size
) ? rbuf
->size
- rbuf
->pwrite
: 0;
158 memcpy(rbuf
->data
+rbuf
->pwrite
, buf
, split
);
163 memcpy(rbuf
->data
+rbuf
->pwrite
, buf
, todo
);
164 rbuf
->pwrite
= (rbuf
->pwrite
+ todo
) % rbuf
->size
;
169 ssize_t
dvb_ringbuffer_pkt_write(struct dvb_ringbuffer
*rbuf
, u8
* buf
, size_t len
)
172 ssize_t oldpwrite
= rbuf
->pwrite
;
174 DVB_RINGBUFFER_WRITE_BYTE(rbuf
, len
>> 8);
175 DVB_RINGBUFFER_WRITE_BYTE(rbuf
, len
& 0xff);
176 DVB_RINGBUFFER_WRITE_BYTE(rbuf
, PKT_READY
);
177 status
= dvb_ringbuffer_write(rbuf
, buf
, len
);
179 if (status
< 0) rbuf
->pwrite
= oldpwrite
;
183 ssize_t
dvb_ringbuffer_pkt_read_user(struct dvb_ringbuffer
*rbuf
, size_t idx
,
184 int offset
, u8 __user
*buf
, size_t len
)
190 pktlen
= rbuf
->data
[idx
] << 8;
191 pktlen
|= rbuf
->data
[(idx
+ 1) % rbuf
->size
];
192 if (offset
> pktlen
) return -EINVAL
;
193 if ((offset
+ len
) > pktlen
) len
= pktlen
- offset
;
195 idx
= (idx
+ DVB_RINGBUFFER_PKTHDRSIZE
+ offset
) % rbuf
->size
;
197 split
= ((idx
+ len
) > rbuf
->size
) ? rbuf
->size
- idx
: 0;
199 if (copy_to_user(buf
, rbuf
->data
+idx
, split
))
205 if (copy_to_user(buf
, rbuf
->data
+idx
, todo
))
211 ssize_t
dvb_ringbuffer_pkt_read(struct dvb_ringbuffer
*rbuf
, size_t idx
,
212 int offset
, u8
* buf
, size_t len
)
218 pktlen
= rbuf
->data
[idx
] << 8;
219 pktlen
|= rbuf
->data
[(idx
+ 1) % rbuf
->size
];
220 if (offset
> pktlen
) return -EINVAL
;
221 if ((offset
+ len
) > pktlen
) len
= pktlen
- offset
;
223 idx
= (idx
+ DVB_RINGBUFFER_PKTHDRSIZE
+ offset
) % rbuf
->size
;
225 split
= ((idx
+ len
) > rbuf
->size
) ? rbuf
->size
- idx
: 0;
227 memcpy(buf
, rbuf
->data
+idx
, split
);
232 memcpy(buf
, rbuf
->data
+idx
, todo
);
236 void dvb_ringbuffer_pkt_dispose(struct dvb_ringbuffer
*rbuf
, size_t idx
)
240 rbuf
->data
[(idx
+ 2) % rbuf
->size
] = PKT_DISPOSED
;
242 // clean up disposed packets
243 while(dvb_ringbuffer_avail(rbuf
) > DVB_RINGBUFFER_PKTHDRSIZE
) {
244 if (DVB_RINGBUFFER_PEEK(rbuf
, 2) == PKT_DISPOSED
) {
245 pktlen
= DVB_RINGBUFFER_PEEK(rbuf
, 0) << 8;
246 pktlen
|= DVB_RINGBUFFER_PEEK(rbuf
, 1);
247 DVB_RINGBUFFER_SKIP(rbuf
, pktlen
+ DVB_RINGBUFFER_PKTHDRSIZE
);
249 // first packet is not disposed, so we stop cleaning now
255 ssize_t
dvb_ringbuffer_pkt_next(struct dvb_ringbuffer
*rbuf
, size_t idx
, size_t* pktlen
)
264 curpktlen
= rbuf
->data
[idx
] << 8;
265 curpktlen
|= rbuf
->data
[(idx
+ 1) % rbuf
->size
];
266 idx
= (idx
+ curpktlen
+ DVB_RINGBUFFER_PKTHDRSIZE
) % rbuf
->size
;
269 consumed
= (idx
- rbuf
->pread
) % rbuf
->size
;
271 while((dvb_ringbuffer_avail(rbuf
) - consumed
) > DVB_RINGBUFFER_PKTHDRSIZE
) {
273 curpktlen
= rbuf
->data
[idx
] << 8;
274 curpktlen
|= rbuf
->data
[(idx
+ 1) % rbuf
->size
];
275 curpktstatus
= rbuf
->data
[(idx
+ 2) % rbuf
->size
];
277 if (curpktstatus
== PKT_READY
) {
282 consumed
+= curpktlen
+ DVB_RINGBUFFER_PKTHDRSIZE
;
283 idx
= (idx
+ curpktlen
+ DVB_RINGBUFFER_PKTHDRSIZE
) % rbuf
->size
;
286 // no packets available
292 EXPORT_SYMBOL(dvb_ringbuffer_init
);
293 EXPORT_SYMBOL(dvb_ringbuffer_empty
);
294 EXPORT_SYMBOL(dvb_ringbuffer_free
);
295 EXPORT_SYMBOL(dvb_ringbuffer_avail
);
296 EXPORT_SYMBOL(dvb_ringbuffer_flush_spinlock_wakeup
);
297 EXPORT_SYMBOL(dvb_ringbuffer_read_user
);
298 EXPORT_SYMBOL(dvb_ringbuffer_read
);
299 EXPORT_SYMBOL(dvb_ringbuffer_write
);