Debian Codegraph @

drivers/dahdi/trunkdev/frame_fifo.c

raw symbols SCIP
1 /*
2 * frame_fifo.c (copied from osmo-e1d)
3 *
4 * (C) 2022 by Harald Welte <[email protected]>
5 *
6 * All Rights Reserved
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
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 * Frame FIFO
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 /*! Initialize a frame FIFO.
39 * \param fifo Caller-allocated memory for FIFO data structure
40 * \param threshold After how many available frames shall we call threshold_cb
41 * \param threshold_cb Optional call-back to call whenever FIFO contains more than 'threshold' frames
42 * \param priv Opaque pointer passed to threshold_cb */
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 /*! Flush (drop) the entire content of the FIFO */
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 /*! put one received frames into the FIFO.
68 * \param fifo The FIFO to which we want to put (append) multiple frames
69 * \param frame Pointer to memory containing the frame data
70 * \param count Number of frames to put into FIFO.
71 * \returns 0 on success; -1 on error (overflow */
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 /* FIXME: detect overflow */
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 /*! put (append) multiple received frames into the FIFO.
101 * \param fifo The FIFO to which we want to put (append) multiple frames
102 * \param frame Pointer to memory containing the frame data
103 * \param count Number of frames to put into FIFO.
104 * \returns Number of frames actually put to FIFO; can be less than 'count' */
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 /* abort on the first failing frame, there's no point in trying further */
114 if (rc < 0)
115 return (int) i;
116 cur += BYTES_PER_FRAME;
117 }
118 return (int) i;
119 }
120
121 /*! pull one frames out of the FIFO.
122 * \param fifo The FIFO from which we want to pull frames
123 * \param out Caller-allocated output buffer
124 * \returns 0 on success; -1 on error (no frame available) */
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 /*! pull multiple frames out of the FIFO.
148 * \param fifo The FIFO from which we want ot pull frames
149 * \param out Caller-allocated output buffer
150 * \param count Number of frames to pull
151 * \returns number of frames pulled; can be 0 or less than count */
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 /* if there's no data in the FIFO, return number of frames
161 * pulled so far, could be 0. */
162 if (rc < 0)
163 return (int) i;
164 cur += BYTES_PER_FRAME;
165 }
166 return (int) i;
167 }