Commit | Line | Data |
---|---|---|
1da177e4 LT |
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. | |
1da177e4 LT |
21 | */ |
22 | ||
23 | ||
24 | ||
1da177e4 LT |
25 | #include <linux/errno.h> |
26 | #include <linux/kernel.h> | |
27 | #include <linux/module.h> | |
28 | #include <linux/sched.h> | |
29 | #include <linux/string.h> | |
7c0f6ba6 | 30 | #include <linux/uaccess.h> |
1da177e4 | 31 | |
fada1935 | 32 | #include <media/dvb_ringbuffer.h> |
1da177e4 LT |
33 | |
34 | #define PKT_READY 0 | |
35 | #define PKT_DISPOSED 1 | |
36 | ||
37 | ||
38 | void dvb_ringbuffer_init(struct dvb_ringbuffer *rbuf, void *data, size_t len) | |
39 | { | |
afd1a0c9 MCC |
40 | rbuf->pread=rbuf->pwrite=0; |
41 | rbuf->data=data; | |
42 | rbuf->size=len; | |
9d9d6bae | 43 | rbuf->error=0; |
1da177e4 | 44 | |
afd1a0c9 | 45 | init_waitqueue_head(&rbuf->queue); |
1da177e4 | 46 | |
afd1a0c9 | 47 | spin_lock_init(&(rbuf->lock)); |
1da177e4 LT |
48 | } |
49 | ||
50 | ||
51 | ||
52 | int dvb_ringbuffer_empty(struct dvb_ringbuffer *rbuf) | |
53 | { | |
ca6e6126 SM |
54 | /* smp_load_acquire() to load write pointer on reader side |
55 | * this pairs with smp_store_release() in dvb_ringbuffer_write(), | |
56 | * dvb_ringbuffer_write_user(), or dvb_ringbuffer_reset() | |
57 | * | |
58 | * for memory barriers also see Documentation/circular-buffers.txt | |
59 | */ | |
60 | return (rbuf->pread == smp_load_acquire(&rbuf->pwrite)); | |
1da177e4 LT |
61 | } |
62 | ||
63 | ||
64 | ||
65 | ssize_t dvb_ringbuffer_free(struct dvb_ringbuffer *rbuf) | |
66 | { | |
afd1a0c9 | 67 | ssize_t free; |
1da177e4 | 68 | |
24fbd6e0 | 69 | /* READ_ONCE() to load read pointer on writer side |
ca6e6126 SM |
70 | * this pairs with smp_store_release() in dvb_ringbuffer_read(), |
71 | * dvb_ringbuffer_read_user(), dvb_ringbuffer_flush(), | |
72 | * or dvb_ringbuffer_reset() | |
73 | */ | |
24fbd6e0 | 74 | free = READ_ONCE(rbuf->pread) - rbuf->pwrite; |
afd1a0c9 MCC |
75 | if (free <= 0) |
76 | free += rbuf->size; | |
77 | return free-1; | |
1da177e4 LT |
78 | } |
79 | ||
80 | ||
81 | ||
82 | ssize_t dvb_ringbuffer_avail(struct dvb_ringbuffer *rbuf) | |
83 | { | |
afd1a0c9 | 84 | ssize_t avail; |
1da177e4 | 85 | |
ca6e6126 SM |
86 | /* smp_load_acquire() to load write pointer on reader side |
87 | * this pairs with smp_store_release() in dvb_ringbuffer_write(), | |
88 | * dvb_ringbuffer_write_user(), or dvb_ringbuffer_reset() | |
89 | */ | |
90 | avail = smp_load_acquire(&rbuf->pwrite) - rbuf->pread; | |
afd1a0c9 MCC |
91 | if (avail < 0) |
92 | avail += rbuf->size; | |
93 | return avail; | |
1da177e4 LT |
94 | } |
95 | ||
96 | ||
97 | ||
98 | void dvb_ringbuffer_flush(struct dvb_ringbuffer *rbuf) | |
99 | { | |
ca6e6126 SM |
100 | /* dvb_ringbuffer_flush() counts as read operation |
101 | * smp_load_acquire() to load write pointer | |
102 | * smp_store_release() to update read pointer, this ensures that the | |
103 | * correct pointer is visible for subsequent dvb_ringbuffer_free() | |
104 | * calls on other cpu cores | |
105 | */ | |
106 | smp_store_release(&rbuf->pread, smp_load_acquire(&rbuf->pwrite)); | |
9d9d6bae | 107 | rbuf->error = 0; |
1da177e4 | 108 | } |
126cd4bc | 109 | EXPORT_SYMBOL(dvb_ringbuffer_flush); |
1da177e4 | 110 | |
48c01a9c AO |
111 | void dvb_ringbuffer_reset(struct dvb_ringbuffer *rbuf) |
112 | { | |
ca6e6126 SM |
113 | /* dvb_ringbuffer_reset() counts as read and write operation |
114 | * smp_store_release() to update read pointer | |
115 | */ | |
116 | smp_store_release(&rbuf->pread, 0); | |
117 | /* smp_store_release() to update write pointer */ | |
118 | smp_store_release(&rbuf->pwrite, 0); | |
48c01a9c AO |
119 | rbuf->error = 0; |
120 | } | |
1da177e4 LT |
121 | |
122 | void dvb_ringbuffer_flush_spinlock_wakeup(struct dvb_ringbuffer *rbuf) | |
123 | { | |
afd1a0c9 | 124 | unsigned long flags; |
1da177e4 | 125 | |
afd1a0c9 MCC |
126 | spin_lock_irqsave(&rbuf->lock, flags); |
127 | dvb_ringbuffer_flush(rbuf); | |
128 | spin_unlock_irqrestore(&rbuf->lock, flags); | |
1da177e4 | 129 | |
afd1a0c9 | 130 | wake_up(&rbuf->queue); |
1da177e4 LT |
131 | } |
132 | ||
b0ba0e3a | 133 | ssize_t dvb_ringbuffer_read_user(struct dvb_ringbuffer *rbuf, u8 __user *buf, size_t len) |
1da177e4 | 134 | { |
afd1a0c9 MCC |
135 | size_t todo = len; |
136 | size_t split; | |
137 | ||
138 | split = (rbuf->pread + len > rbuf->size) ? rbuf->size - rbuf->pread : 0; | |
139 | if (split > 0) { | |
b0ba0e3a AV |
140 | if (copy_to_user(buf, rbuf->data+rbuf->pread, split)) |
141 | return -EFAULT; | |
afd1a0c9 MCC |
142 | buf += split; |
143 | todo -= split; | |
ca6e6126 SM |
144 | /* smp_store_release() for read pointer update to ensure |
145 | * that buf is not overwritten until read is complete, | |
24fbd6e0 | 146 | * this pairs with READ_ONCE() in dvb_ringbuffer_free() |
ca6e6126 SM |
147 | */ |
148 | smp_store_release(&rbuf->pread, 0); | |
afd1a0c9 | 149 | } |
b0ba0e3a AV |
150 | if (copy_to_user(buf, rbuf->data+rbuf->pread, todo)) |
151 | return -EFAULT; | |
afd1a0c9 | 152 | |
ca6e6126 SM |
153 | /* smp_store_release() to update read pointer, see above */ |
154 | smp_store_release(&rbuf->pread, (rbuf->pread + todo) % rbuf->size); | |
afd1a0c9 MCC |
155 | |
156 | return len; | |
1da177e4 LT |
157 | } |
158 | ||
b0ba0e3a AV |
159 | void dvb_ringbuffer_read(struct dvb_ringbuffer *rbuf, u8 *buf, size_t len) |
160 | { | |
161 | size_t todo = len; | |
162 | size_t split; | |
163 | ||
164 | split = (rbuf->pread + len > rbuf->size) ? rbuf->size - rbuf->pread : 0; | |
165 | if (split > 0) { | |
166 | memcpy(buf, rbuf->data+rbuf->pread, split); | |
167 | buf += split; | |
168 | todo -= split; | |
ca6e6126 SM |
169 | /* smp_store_release() for read pointer update to ensure |
170 | * that buf is not overwritten until read is complete, | |
24fbd6e0 | 171 | * this pairs with READ_ONCE() in dvb_ringbuffer_free() |
ca6e6126 SM |
172 | */ |
173 | smp_store_release(&rbuf->pread, 0); | |
b0ba0e3a AV |
174 | } |
175 | memcpy(buf, rbuf->data+rbuf->pread, todo); | |
176 | ||
ca6e6126 SM |
177 | /* smp_store_release() to update read pointer, see above */ |
178 | smp_store_release(&rbuf->pread, (rbuf->pread + todo) % rbuf->size); | |
b0ba0e3a | 179 | } |
1da177e4 LT |
180 | |
181 | ||
182 | ssize_t dvb_ringbuffer_write(struct dvb_ringbuffer *rbuf, const u8 *buf, size_t len) | |
183 | { | |
afd1a0c9 MCC |
184 | size_t todo = len; |
185 | size_t split; | |
1da177e4 | 186 | |
afd1a0c9 | 187 | split = (rbuf->pwrite + len > rbuf->size) ? rbuf->size - rbuf->pwrite : 0; |
1da177e4 | 188 | |
afd1a0c9 MCC |
189 | if (split > 0) { |
190 | memcpy(rbuf->data+rbuf->pwrite, buf, split); | |
191 | buf += split; | |
192 | todo -= split; | |
ca6e6126 SM |
193 | /* smp_store_release() for write pointer update to ensure that |
194 | * written data is visible on other cpu cores before the pointer | |
195 | * update, this pairs with smp_load_acquire() in | |
196 | * dvb_ringbuffer_empty() or dvb_ringbuffer_avail() | |
197 | */ | |
198 | smp_store_release(&rbuf->pwrite, 0); | |
afd1a0c9 MCC |
199 | } |
200 | memcpy(rbuf->data+rbuf->pwrite, buf, todo); | |
ca6e6126 SM |
201 | /* smp_store_release() for write pointer update, see above */ |
202 | smp_store_release(&rbuf->pwrite, (rbuf->pwrite + todo) % rbuf->size); | |
1da177e4 | 203 | |
afd1a0c9 | 204 | return len; |
1da177e4 LT |
205 | } |
206 | ||
04da2dae MCC |
207 | ssize_t dvb_ringbuffer_write_user(struct dvb_ringbuffer *rbuf, |
208 | const u8 __user *buf, size_t len) | |
209 | { | |
210 | int status; | |
211 | size_t todo = len; | |
212 | size_t split; | |
213 | ||
214 | split = (rbuf->pwrite + len > rbuf->size) ? rbuf->size - rbuf->pwrite : 0; | |
215 | ||
216 | if (split > 0) { | |
217 | status = copy_from_user(rbuf->data+rbuf->pwrite, buf, split); | |
218 | if (status) | |
219 | return len - todo; | |
220 | buf += split; | |
221 | todo -= split; | |
ca6e6126 SM |
222 | /* smp_store_release() for write pointer update to ensure that |
223 | * written data is visible on other cpu cores before the pointer | |
224 | * update, this pairs with smp_load_acquire() in | |
225 | * dvb_ringbuffer_empty() or dvb_ringbuffer_avail() | |
226 | */ | |
227 | smp_store_release(&rbuf->pwrite, 0); | |
04da2dae MCC |
228 | } |
229 | status = copy_from_user(rbuf->data+rbuf->pwrite, buf, todo); | |
230 | if (status) | |
231 | return len - todo; | |
ca6e6126 SM |
232 | /* smp_store_release() for write pointer update, see above */ |
233 | smp_store_release(&rbuf->pwrite, (rbuf->pwrite + todo) % rbuf->size); | |
04da2dae MCC |
234 | |
235 | return len; | |
236 | } | |
237 | ||
1da177e4 LT |
238 | ssize_t dvb_ringbuffer_pkt_write(struct dvb_ringbuffer *rbuf, u8* buf, size_t len) |
239 | { | |
afd1a0c9 MCC |
240 | int status; |
241 | ssize_t oldpwrite = rbuf->pwrite; | |
1da177e4 | 242 | |
afd1a0c9 MCC |
243 | DVB_RINGBUFFER_WRITE_BYTE(rbuf, len >> 8); |
244 | DVB_RINGBUFFER_WRITE_BYTE(rbuf, len & 0xff); | |
245 | DVB_RINGBUFFER_WRITE_BYTE(rbuf, PKT_READY); | |
246 | status = dvb_ringbuffer_write(rbuf, buf, len); | |
1da177e4 | 247 | |
afd1a0c9 MCC |
248 | if (status < 0) rbuf->pwrite = oldpwrite; |
249 | return status; | |
1da177e4 LT |
250 | } |
251 | ||
b0ba0e3a AV |
252 | ssize_t dvb_ringbuffer_pkt_read_user(struct dvb_ringbuffer *rbuf, size_t idx, |
253 | int offset, u8 __user *buf, size_t len) | |
1da177e4 | 254 | { |
afd1a0c9 MCC |
255 | size_t todo; |
256 | size_t split; | |
257 | size_t pktlen; | |
258 | ||
259 | pktlen = rbuf->data[idx] << 8; | |
260 | pktlen |= rbuf->data[(idx + 1) % rbuf->size]; | |
261 | if (offset > pktlen) return -EINVAL; | |
262 | if ((offset + len) > pktlen) len = pktlen - offset; | |
263 | ||
264 | idx = (idx + DVB_RINGBUFFER_PKTHDRSIZE + offset) % rbuf->size; | |
265 | todo = len; | |
266 | split = ((idx + len) > rbuf->size) ? rbuf->size - idx : 0; | |
267 | if (split > 0) { | |
b0ba0e3a AV |
268 | if (copy_to_user(buf, rbuf->data+idx, split)) |
269 | return -EFAULT; | |
afd1a0c9 MCC |
270 | buf += split; |
271 | todo -= split; | |
272 | idx = 0; | |
273 | } | |
b0ba0e3a AV |
274 | if (copy_to_user(buf, rbuf->data+idx, todo)) |
275 | return -EFAULT; | |
276 | ||
277 | return len; | |
278 | } | |
afd1a0c9 | 279 | |
b0ba0e3a AV |
280 | ssize_t dvb_ringbuffer_pkt_read(struct dvb_ringbuffer *rbuf, size_t idx, |
281 | int offset, u8* buf, size_t len) | |
282 | { | |
283 | size_t todo; | |
284 | size_t split; | |
285 | size_t pktlen; | |
286 | ||
287 | pktlen = rbuf->data[idx] << 8; | |
288 | pktlen |= rbuf->data[(idx + 1) % rbuf->size]; | |
289 | if (offset > pktlen) return -EINVAL; | |
290 | if ((offset + len) > pktlen) len = pktlen - offset; | |
291 | ||
292 | idx = (idx + DVB_RINGBUFFER_PKTHDRSIZE + offset) % rbuf->size; | |
293 | todo = len; | |
294 | split = ((idx + len) > rbuf->size) ? rbuf->size - idx : 0; | |
295 | if (split > 0) { | |
296 | memcpy(buf, rbuf->data+idx, split); | |
297 | buf += split; | |
298 | todo -= split; | |
299 | idx = 0; | |
300 | } | |
301 | memcpy(buf, rbuf->data+idx, todo); | |
afd1a0c9 | 302 | return len; |
1da177e4 LT |
303 | } |
304 | ||
305 | void dvb_ringbuffer_pkt_dispose(struct dvb_ringbuffer *rbuf, size_t idx) | |
306 | { | |
afd1a0c9 MCC |
307 | size_t pktlen; |
308 | ||
309 | rbuf->data[(idx + 2) % rbuf->size] = PKT_DISPOSED; | |
310 | ||
311 | // clean up disposed packets | |
312 | while(dvb_ringbuffer_avail(rbuf) > DVB_RINGBUFFER_PKTHDRSIZE) { | |
313 | if (DVB_RINGBUFFER_PEEK(rbuf, 2) == PKT_DISPOSED) { | |
50c25fff MK |
314 | pktlen = DVB_RINGBUFFER_PEEK(rbuf, 0) << 8; |
315 | pktlen |= DVB_RINGBUFFER_PEEK(rbuf, 1); | |
316 | DVB_RINGBUFFER_SKIP(rbuf, pktlen + DVB_RINGBUFFER_PKTHDRSIZE); | |
afd1a0c9 | 317 | } else { |
50c25fff MK |
318 | // first packet is not disposed, so we stop cleaning now |
319 | break; | |
afd1a0c9 MCC |
320 | } |
321 | } | |
1da177e4 LT |
322 | } |
323 | ||
324 | ssize_t dvb_ringbuffer_pkt_next(struct dvb_ringbuffer *rbuf, size_t idx, size_t* pktlen) | |
325 | { | |
afd1a0c9 MCC |
326 | int consumed; |
327 | int curpktlen; | |
328 | int curpktstatus; | |
1da177e4 | 329 | |
afd1a0c9 | 330 | if (idx == -1) { |
1da177e4 LT |
331 | idx = rbuf->pread; |
332 | } else { | |
afd1a0c9 MCC |
333 | curpktlen = rbuf->data[idx] << 8; |
334 | curpktlen |= rbuf->data[(idx + 1) % rbuf->size]; | |
335 | idx = (idx + curpktlen + DVB_RINGBUFFER_PKTHDRSIZE) % rbuf->size; | |
1da177e4 LT |
336 | } |
337 | ||
afd1a0c9 | 338 | consumed = (idx - rbuf->pread) % rbuf->size; |
1da177e4 | 339 | |
afd1a0c9 | 340 | while((dvb_ringbuffer_avail(rbuf) - consumed) > DVB_RINGBUFFER_PKTHDRSIZE) { |
1da177e4 | 341 | |
afd1a0c9 MCC |
342 | curpktlen = rbuf->data[idx] << 8; |
343 | curpktlen |= rbuf->data[(idx + 1) % rbuf->size]; | |
344 | curpktstatus = rbuf->data[(idx + 2) % rbuf->size]; | |
1da177e4 | 345 | |
afd1a0c9 | 346 | if (curpktstatus == PKT_READY) { |
50c25fff MK |
347 | *pktlen = curpktlen; |
348 | return idx; | |
afd1a0c9 | 349 | } |
1da177e4 | 350 | |
afd1a0c9 MCC |
351 | consumed += curpktlen + DVB_RINGBUFFER_PKTHDRSIZE; |
352 | idx = (idx + curpktlen + DVB_RINGBUFFER_PKTHDRSIZE) % rbuf->size; | |
353 | } | |
1da177e4 | 354 | |
afd1a0c9 MCC |
355 | // no packets available |
356 | return -1; | |
1da177e4 LT |
357 | } |
358 | ||
359 | ||
360 | ||
361 | EXPORT_SYMBOL(dvb_ringbuffer_init); | |
362 | EXPORT_SYMBOL(dvb_ringbuffer_empty); | |
363 | EXPORT_SYMBOL(dvb_ringbuffer_free); | |
364 | EXPORT_SYMBOL(dvb_ringbuffer_avail); | |
1da177e4 | 365 | EXPORT_SYMBOL(dvb_ringbuffer_flush_spinlock_wakeup); |
b0ba0e3a | 366 | EXPORT_SYMBOL(dvb_ringbuffer_read_user); |
1da177e4 LT |
367 | EXPORT_SYMBOL(dvb_ringbuffer_read); |
368 | EXPORT_SYMBOL(dvb_ringbuffer_write); | |
04da2dae | 369 | EXPORT_SYMBOL(dvb_ringbuffer_write_user); |