| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <linux/types.h> |
| 22 |
#include <linux/errno.h> |
| 23 |
#include <linux/string.h> |
| 24 |
|
| 25 |
#include "frame_fifo.h" |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
void fifo_dump(struct frame_fifo *fifo) |
| 33 |
{ |
| 34 |
printk(KERN_DEBUG "buf=%p, size=%zu, next_in=%lu, next_out=%lu\n", fifo->buf, sizeof(fifo->buf), |
| 35 |
fifo->next_in - fifo->buf, fifo->next_out - fifo->buf); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
void frame_fifo_init(struct frame_fifo *fifo, unsigned int threshold, |
| 44 |
void (*threshold_cb)(struct frame_fifo *fifo, unsigned int frames, void *priv), |
| 45 |
void *priv) |
| 46 |
{ |
| 47 |
memset(fifo->buf, 0xff, sizeof(fifo->buf)); |
| 48 |
fifo->next_in = fifo->buf; |
| 49 |
fifo->next_out = fifo->buf; |
| 50 |
fifo->threshold = threshold; |
| 51 |
fifo->threshold_cb = threshold_cb; |
| 52 |
fifo->priv = priv; |
| 53 |
spin_lock_init(&fifo->lock); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
void frame_fifo_flush(struct frame_fifo *fifo) |
| 58 |
{ |
| 59 |
unsigned long flags; |
| 60 |
spin_lock_irqsave(&fifo->lock, flags); |
| 61 |
fifo->next_out = fifo->next_in; |
| 62 |
spin_unlock_irqrestore(&fifo->lock, flags); |
| 63 |
} |
| 64 |
|
| 65 |
#define FIFO_BUF_END(f) ((f)->buf + sizeof((f)->buf)) |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
int frame_fifo_in(struct frame_fifo *fifo, const uint8_t *frame) |
| 73 |
{ |
| 74 |
unsigned long flags; |
| 75 |
|
| 76 |
spin_lock_irqsave(&fifo->lock, flags); |
| 77 |
#if 0 |
| 78 |
|
| 79 |
if (fifo->next_in == fifo->next_out) { |
| 80 |
spin_unlock_irqrestore(&fifo->lock, flags); |
| 81 |
return -ENOSPC; |
| 82 |
} |
| 83 |
#endif |
| 84 |
BUG_ON(fifo->next_in + BYTES_PER_FRAME > FIFO_BUF_END(fifo)); |
| 85 |
memcpy(fifo->next_in, frame, BYTES_PER_FRAME); |
| 86 |
fifo->next_in += BYTES_PER_FRAME; |
| 87 |
if (fifo->next_in >= FIFO_BUF_END(fifo)) |
| 88 |
fifo->next_in -= sizeof(fifo->buf); |
| 89 |
spin_unlock_irqrestore(&fifo->lock, flags); |
| 90 |
|
| 91 |
if (fifo->threshold_cb) { |
| 92 |
unsigned int frames_avail = frame_fifo_frames(fifo); |
| 93 |
if (frames_avail >= fifo->threshold) |
| 94 |
fifo->threshold_cb(fifo, frames_avail, fifo->priv); |
| 95 |
} |
| 96 |
|
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
int frame_fifo_in_multi(struct frame_fifo *fifo, const uint8_t *frame, size_t count) |
| 106 |
{ |
| 107 |
const uint8_t *cur = frame; |
| 108 |
unsigned int i; |
| 109 |
int rc; |
| 110 |
|
| 111 |
for (i = 0; i < count; i++) { |
| 112 |
rc = frame_fifo_in(fifo, cur); |
| 113 |
|
| 114 |
if (rc < 0) |
| 115 |
return (int) i; |
| 116 |
cur += BYTES_PER_FRAME; |
| 117 |
} |
| 118 |
return (int) i; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
int frame_fifo_out(struct frame_fifo *fifo, uint8_t *out) |
| 126 |
{ |
| 127 |
unsigned long flags; |
| 128 |
|
| 129 |
spin_lock_irqsave(&fifo->lock, flags); |
| 130 |
|
| 131 |
if (__frame_fifo_frames(fifo) < 1) { |
| 132 |
spin_unlock_irqrestore(&fifo->lock, flags); |
| 133 |
return -1; |
| 134 |
} |
| 135 |
|
| 136 |
memcpy(out, fifo->next_out, BYTES_PER_FRAME); |
| 137 |
fifo->next_out += BYTES_PER_FRAME; |
| 138 |
|
| 139 |
if (fifo->next_out >= FIFO_BUF_END(fifo)) |
| 140 |
fifo->next_out -= sizeof(fifo->buf); |
| 141 |
|
| 142 |
spin_unlock_irqrestore(&fifo->lock, flags); |
| 143 |
|
| 144 |
return 0; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
int frame_fifo_out_multi(struct frame_fifo *fifo, uint8_t *out, size_t count) |
| 153 |
{ |
| 154 |
uint8_t *cur = out; |
| 155 |
unsigned int i; |
| 156 |
int rc = 0; |
| 157 |
|
| 158 |
for (i = 0; i < count; i++) { |
| 159 |
rc = frame_fifo_out(fifo, cur); |
| 160 |
|
| 161 |
|
| 162 |
if (rc < 0) |
| 163 |
return (int) i; |
| 164 |
cur += BYTES_PER_FRAME; |
| 165 |
} |
| 166 |
return (int) i; |
| 167 |
} |