Debian Codegraph @

drivers/dahdi/zaphfc/fifo.c

raw symbols SCIP
1 /*
2 * fifo.c - HFC FIFO management routines
3 *
4 * Copyright (C) 2006 headissue GmbH; Jens Wilke
5 * Copyright (C) 2004 Daniele Orlandi
6 * Copyright (C) 2002, 2003, 2004, Junghanns.NET GmbH
7 *
8 * Original author of this code is
9 * Daniele "Vihai" Orlandi <[email protected]>
10 *
11 * This program is free software and may be modified and
12 * distributed under the terms of the GNU General Public License.
13 *
14 */
15
16 #include <linux/kernel.h>
17
18 #include <dahdi/kernel.h>
19
20 #include "fifo.h"
21
22 static void hfc_fifo_mem_read(struct hfc_chan_simplex *chan,
23 int z_start,
24 void *data, int size)
25 {
26 int bytes_to_boundary = chan->z_max - z_start + 1;
27 if (bytes_to_boundary >= size) {
28 memcpy(data,
29 chan->z_base + z_start,
30 size);
31 } else {
32 /*
33 * Buffer wrap
34 */
35 memcpy(data,
36 chan->z_base + z_start,
37 bytes_to_boundary);
38
39 memcpy(data + bytes_to_boundary,
40 chan->fifo_base,
41 size - bytes_to_boundary);
42 }
43 }
44
45 static void hfc_fifo_mem_write(struct hfc_chan_simplex *chan,
46 void *data, int size)
47 {
48 int bytes_to_boundary = chan->z_max - *Z1_F1(chan) + 1;
49 if (bytes_to_boundary >= size) {
50 memcpy(chan->z_base + *Z1_F1(chan),
51 data,
52 size);
53 } else {
54 /*
55 * FIFO wrap
56 */
57
58 memcpy(chan->z_base + *Z1_F1(chan),
59 data,
60 bytes_to_boundary);
61
62 memcpy(chan->fifo_base,
63 data + bytes_to_boundary,
64 size - bytes_to_boundary);
65 }
66 }
67
68 int hfc_fifo_get(struct hfc_chan_simplex *chan,
69 void *data, int size)
70 {
71 int available_bytes;
72
73 /*
74 * Some useless statistic
75 */
76 chan->bytes += size;
77
78 available_bytes = hfc_fifo_used_rx(chan);
79
80 if (available_bytes < size && !chan->fifo_underrun++) {
81 /*
82 * print the warning only once
83 */
84 printk(KERN_WARNING hfc_DRIVER_PREFIX
85 "card %d: "
86 "chan %s: "
87 "RX FIFO not enough (%d) bytes to receive!\n",
88 chan->chan->card->cardnum,
89 chan->chan->name,
90 available_bytes);
91 return -1;
92 }
93
94 hfc_fifo_mem_read(chan, *Z2_F2(chan), data, size);
95 *Z2_F2(chan) = Z_inc(chan, *Z2_F2(chan), size);
96 return available_bytes - size;
97 }
98
99 void hfc_fifo_put(struct hfc_chan_simplex *chan,
100 void *data, int size)
101 {
102 struct hfc_card *card = chan->chan->card;
103 int used_bytes = hfc_fifo_used_tx(chan);
104 int free_bytes = hfc_fifo_free_tx(chan);
105
106 if (!used_bytes && !chan->fifo_underrun++) {
107 /*
108 * print warning only once, to make timing not worse
109 */
110 printk(KERN_WARNING hfc_DRIVER_PREFIX
111 "card %d: "
112 "chan %s: "
113 "TX FIFO has become empty\n",
114 card->cardnum,
115 chan->chan->name);
116 }
117 if (free_bytes < size) {
118 printk(KERN_CRIT hfc_DRIVER_PREFIX
119 "card %d: "
120 "chan %s: "
121 "TX FIFO full!\n",
122 chan->chan->card->cardnum,
123 chan->chan->name);
124 chan->fifo_full++;
125 hfc_clear_fifo_tx(chan);
126 }
127
128 hfc_fifo_mem_write(chan, data, size);
129 chan->bytes += size;
130 *Z1_F1(chan) = Z_inc(chan, *Z1_F1(chan), size);
131 }
132
133 int hfc_fifo_get_frame(struct hfc_chan_simplex *chan, void *data, int max_size)
134 {
135 int frame_size;
136 u16 newz2 ;
137
138 if (*chan->f1 == *chan->f2) {
139 /*
140 * nothing received, strange uh?
141 */
142 printk(KERN_WARNING hfc_DRIVER_PREFIX
143 "card %d: "
144 "chan %s: "
145 "get_frame called with no frame in FIFO.\n",
146 chan->chan->card->cardnum,
147 chan->chan->name);
148
149 return -1;
150 }
151
152 /*
153 * frame_size includes CRC+CRC+STAT
154 */
155 frame_size = hfc_fifo_get_frame_size(chan);
156
157 #ifdef DEBUG
158 if (debug_level == 3) {
159 printk(KERN_DEBUG hfc_DRIVER_PREFIX
160 "card %d: "
161 "chan %s: "
162 "RX len %2d: ",
163 chan->chan->card->cardnum,
164 chan->chan->name,
165 frame_size);
166 } else if (debug_level >= 4) {
167 printk(KERN_DEBUG hfc_DRIVER_PREFIX
168 "card %d: "
169 "chan %s: "
170 "RX (f1=%02x, f2=%02x, z1=%04x, z2=%04x) len %2d: ",
171 chan->chan->card->cardnum,
172 chan->chan->name,
173 *chan->f1, *chan->f2, *Z1_F2(chan), *Z2_F2(chan),
174 frame_size);
175 }
176
177 if (debug_level >= 3) {
178 int i;
179 for (i = 0; i < frame_size; i++) {
180 printk("%02x", hfc_fifo_u8(chan,
181 Z_inc(chan, *Z2_F2(chan), i)));
182 }
183
184 printk("\n");
185 }
186 #endif
187
188 if (frame_size <= 0) {
189 #ifdef DEBUG
190 if (debug_level >= 2) {
191 printk(KERN_DEBUG hfc_DRIVER_PREFIX
192 "card %d: "
193 "chan %s: "
194 "invalid (empty) frame received.\n",
195 chan->chan->card->cardnum,
196 chan->chan->name);
197 }
198 #endif
199
200 hfc_fifo_drop_frame(chan);
201 return -1;
202 }
203
204 /*
205 * STAT is not really received
206 */
207 chan->bytes += frame_size - 1;
208
209 /*
210 * Calculate beginning of the next frame
211 */
212 newz2 = Z_inc(chan, *Z2_F2(chan), frame_size);
213
214 /*
215 * We cannot use hfc_fifo_get because of different semantic of
216 * "available bytes" and to avoid useless increment of Z2
217 */
218 hfc_fifo_mem_read(chan, *Z2_F2(chan), data,
219 frame_size < max_size ? frame_size : max_size);
220
221 if (hfc_fifo_u8(chan, Z_inc(chan, *Z2_F2(chan),
222 frame_size - 1)) != 0x00) {
223 /*
224 * CRC not ok, frame broken, skipping
225 */
226 #ifdef DEBUG
227 if (debug_level >= 2) {
228 printk(KERN_WARNING hfc_DRIVER_PREFIX
229 "card %d: "
230 "chan %s: "
231 "Received frame with wrong CRC\n",
232 chan->chan->card->cardnum,
233 chan->chan->name);
234 }
235 #endif
236
237 chan->crc++;
238
239 hfc_fifo_drop_frame(chan);
240 return -1;
241 }
242
243 chan->frames++;
244
245 *chan->f2 = F_inc(chan, *chan->f2, 1);
246
247 /*
248 * Set Z2 for the next frame we're going to receive
249 */
250 *Z2_F2(chan) = newz2;
251
252 return frame_size;
253 }
254
255 void hfc_fifo_drop_frame(struct hfc_chan_simplex *chan)
256 {
257 int available_bytes;
258 u16 newz2;
259
260 if (*chan->f1 == *chan->f2) {
261 /*
262 * nothing received, strange eh?
263 */
264 printk(KERN_WARNING hfc_DRIVER_PREFIX
265 "card %d: "
266 "chan %s: "
267 "skip_frame called with no frame in FIFO.\n",
268 chan->chan->card->cardnum,
269 chan->chan->name);
270
271 return;
272 }
273
274 available_bytes = hfc_fifo_used_rx(chan) + 1;
275
276 /*
277 * Calculate beginning of the next frame
278 */
279 newz2 = Z_inc(chan, *Z2_F2(chan), available_bytes);
280
281 *chan->f2 = F_inc(chan, *chan->f2, 1);
282
283 /*
284 * Set Z2 for the next frame we're going to receive
285 */
286 *Z2_F2(chan) = newz2;
287 }
288
289 void hfc_fifo_put_frame(struct hfc_chan_simplex *chan,
290 void *data, int size)
291 {
292 u16 newz1;
293 int available_frames;
294
295 #ifdef DEBUG
296 if (debug_level == 3) {
297 printk(KERN_DEBUG hfc_DRIVER_PREFIX
298 "card %d: "
299 "chan %s: "
300 "TX len %2d: ",
301 chan->chan->card->cardnum,
302 chan->chan->name,
303 size);
304 } else if (debug_level >= 4) {
305 printk(KERN_DEBUG hfc_DRIVER_PREFIX
306 "card %d: "
307 "chan %s: "
308 "TX (f1=%02x, f2=%02x, z1=%04x, z2=%04x) len %2d: ",
309 chan->chan->card->cardnum,
310 chan->chan->name,
311 *chan->f1, *chan->f2, *Z1_F1(chan), *Z2_F1(chan),
312 size);
313 }
314
315 if (debug_level >= 3) {
316 int i;
317 for (i = 0; i < size; i++)
318 printk("%02x", ((u8 *)data)[i]);
319
320 printk("\n");
321 }
322 #endif
323
324 available_frames = hfc_fifo_free_frames(chan);
325
326 if (available_frames >= chan->f_num) {
327 printk(KERN_CRIT hfc_DRIVER_PREFIX
328 "card %d: "
329 "chan %s: "
330 "TX FIFO total number of frames exceeded!\n",
331 chan->chan->card->cardnum,
332 chan->chan->name);
333
334 chan->fifo_full++;
335
336 return;
337 }
338
339 hfc_fifo_put(chan, data, size);
340
341 newz1 = *Z1_F1(chan);
342
343 *chan->f1 = F_inc(chan, *chan->f1, 1);
344
345 *Z1_F1(chan) = newz1;
346
347 chan->frames++;
348 }
349
350 void hfc_clear_fifo_rx(struct hfc_chan_simplex *chan)
351 {
352 *chan->f2 = *chan->f1;
353 *Z2_F2(chan) = *Z1_F2(chan);
354 }
355
356 void hfc_clear_fifo_tx(struct hfc_chan_simplex *chan)
357 {
358 *chan->f1 = *chan->f2;
359 *Z1_F1(chan) = *Z2_F1(chan);
360
361 if (chan->chan->status == open_voice) {
362 /*
363 * Make sure that at least hfc_TX_FIFO_PRELOAD bytes are
364 * present in the TX FIFOs
365 * Create hfc_TX_FIFO_PRELOAD bytes of empty data
366 * (0x7f is mute audio)
367 */
368 u8 empty_fifo[hfc_TX_FIFO_PRELOAD +
369 DAHDI_CHUNKSIZE + hfc_RX_FIFO_PRELOAD];
370 memset(empty_fifo, 0x7f, sizeof(empty_fifo));
371
372 hfc_fifo_put(chan, empty_fifo, sizeof(empty_fifo));
373 }
374 }
375