Debian Codegraph @

drivers/dahdi/xpp/xbus-pcm.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * Written by Oron Peled <[email protected]>
3 * Copyright (C) 2004-2007, Xorcom
4 *
5 * All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 */
22 #include <linux/version.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include "xbus-pcm.h"
26 #include "xbus-core.h"
27 #include "xpp_dahdi.h"
28 #include "dahdi_debug.h"
29 #include "parport_debug.h"
30
31 static const char rcsid[] = "$Id$";
32
33 extern int debug;
34 #ifdef OPTIMIZE_CHANMUTE
35 static DEF_PARM_BOOL(optimize_chanmute, 1, 0644,
36 "Optimize by muting inactive channels");
37 #endif
38
39 static DEF_PARM(int, disable_pcm, 0, 0644, "Disable all PCM transmissions");
40 #ifdef DEBUG_PCMTX
41 DEF_PARM(int, pcmtx, -1, 0644,
42 "Forced PCM value to transmit (negative to disable)");
43 EXPORT_SYMBOL(pcmtx);
44 DEF_PARM(int, pcmtx_chan, 0, 0644, "channel to force PCM value");
45 EXPORT_SYMBOL(pcmtx_chan);
46 #endif
47 static DEF_PARM_BOOL(disable_pll_sync, 0, 0644,
48 "Disable automatic adjustment of AB clocks");
49
50 static xbus_t *syncer; /* current syncer */
51 static atomic_t xpp_tick_counter = ATOMIC_INIT(0);
52 static struct xpp_ticker dahdi_ticker;
53 /*
54 * The ref_ticker points to the current referece tick source.
55 * I.e: one of our AB or dahdi_ticker
56 */
57 static struct xpp_ticker *ref_ticker;
58 static DEFINE_SPINLOCK(ref_ticker_lock);
59 static DEFINE_SPINLOCK(elect_syncer_lock);
60 /* from /sys/bus/astribanks/drivers/xppdrv/sync */
61 static bool force_dahdi_sync;
62 static xbus_t *global_ticker;
63 static struct xpp_ticker global_ticks_series;
64
65 #define PROC_SYNC "sync"
66 /* Sampling cycle in usec */
67 #define SYNC_CYCLE 500
68 /* Samples from end of SYNC_CYCLE */
69 #define SYNC_CYCLE_SAMPLE 100
70 /* Number of SYNC_CYCLE's to converge speed */
71 #define SYNC_CONVERGE 10
72 /* Offset from ref_ticker to other AB's */
73 #define SYNC_CENTER 500
74 /* If within +/-SYNC_DELTA, try to stay there */
75 #define SYNC_DELTA 40
76 #define BIG_TICK_INTERVAL 1000
77 /* maximal firmware drift unit (hardware limit 63) */
78 #define SYNC_ADJ_MAX 20
79
80 /*
81 * The USB bulk endpoints have a large jitter in the timing of frames
82 * from the AB to the ehci-hcd. This is because we cannot predict
83 * in which USB micro-frame our data passes. Each micro-frame is
84 * A 125 usec.
85 */
86 #define SYNC_ADJ_QUICK 1000
87 #define SYNC_ADJ_SLOW 10000
88
89 #ifdef DAHDI_SYNC_TICK
90 static unsigned int dahdi_tick_count;
91 #endif
92
93 /*------------------------- SYNC Handling --------------------------*/
94
95 static void send_drift(xbus_t *xbus, int drift);
96
97 static void ticker_set_cycle(struct xpp_ticker *ticker, int cycle)
98 {
99 unsigned long flags;
100
101 spin_lock_irqsave(&ticker->lock, flags);
102 if (cycle < SYNC_ADJ_QUICK)
103 cycle = SYNC_ADJ_QUICK;
104 if (cycle > SYNC_ADJ_SLOW)
105 cycle = SYNC_ADJ_SLOW;
106 ticker->cycle = cycle;
107 spin_unlock_irqrestore(&ticker->lock, flags);
108 }
109
110 static void xpp_ticker_init(struct xpp_ticker *ticker)
111 {
112 memset(ticker, 0, sizeof(*ticker));
113 spin_lock_init(&ticker->lock);
114 ticker->last_sample = ktime_get();
115 ticker->first_sample = ticker->last_sample;
116 ticker_set_cycle(ticker, SYNC_ADJ_QUICK);
117 }
118
119 static int xpp_ticker_step(struct xpp_ticker *ticker, const ktime_t t)
120 {
121 unsigned long flags;
122 s64 usec;
123 bool cycled = 0;
124
125 spin_lock_irqsave(&ticker->lock, flags);
126 ticker->last_sample = t;
127 /* rate adjust */
128 if ((ticker->count % ticker->cycle) == ticker->cycle - 1) {
129 usec = ktime_us_delta(ticker->last_sample,
130 ticker->first_sample);
131 ticker->first_sample = ticker->last_sample;
132 ticker->tick_period = usec / ticker->cycle;
133 cycled = 1;
134 }
135 ticker->count++;
136 spin_unlock_irqrestore(&ticker->lock, flags);
137 return cycled;
138 }
139
140 /*
141 * No locking. It is called only from:
142 * - update_sync_master() in a globall spinlock protected code.
143 * - initalization.
144 */
145 static inline void xbus_drift_clear(xbus_t *xbus)
146 {
147 struct xpp_drift *di = &xbus->drift;
148
149 di->last_lost_tick = ktime_get();
150 ticker_set_cycle(&xbus->ticker, SYNC_ADJ_QUICK);
151 di->max_speed = -SYNC_ADJ_MAX;
152 di->min_speed = SYNC_ADJ_MAX;
153 }
154
155 void xpp_drift_init(xbus_t *xbus)
156 {
157 memset(&xbus->drift, 0, sizeof(xbus->drift));
158 spin_lock_init(&xbus->drift.lock);
159 xpp_ticker_init(&xbus->ticker);
160 xbus_drift_clear(xbus);
161 }
162
163 #ifdef SAMPLE_TICKS
164 static void sample_tick(xbus_t *xbus, int sample)
165 {
166 if (!xbus->sample_running)
167 return;
168 if (xbus->sample_pos < SAMPLE_SIZE)
169 xbus->sample_ticks[xbus->sample_pos++] = sample;
170 else {
171 xbus->sample_running = 0;
172 xbus->sample_pos = 0;
173 }
174 }
175 #else
176 #define sample_tick(x, y)
177 #endif
178
179 /*
180 * The following function adjust the clock of an astribank according
181 * to our reference clock (another astribank or another DAHDI device).
182 *
183 * It is VERY hard to stabilise these corrections:
184 * - The measurments are affected by 125usec USB micro-frames.
185 * - However, if we are off by more than +/-500usec we are risking
186 * loosing frames.
187 *
188 * Every change must be tested rigorously with many device combinations
189 * including abrupt changes in sync -- to verify the stability of the
190 * algorithm.
191 */
192 static void xpp_drift_step(xbus_t *xbus, const ktime_t kt)
193 {
194 struct xpp_drift *di = &xbus->drift;
195 struct xpp_ticker *ticker = &xbus->ticker;
196 unsigned long flags;
197
198 spin_lock_irqsave(&di->lock, flags);
199 xpp_ticker_step(&xbus->ticker, kt);
200 /*
201 * Do we need to be synchronized and is there an established reference
202 * ticker (another Astribank or another DAHDI device) already?
203 */
204 if (ref_ticker && ref_ticker != &xbus->ticker && syncer
205 && xbus->sync_mode == SYNC_MODE_PLL) {
206 int new_delta_tick = ticker->count - ref_ticker->count;
207 int lost_ticks = new_delta_tick - di->delta_tick;
208 s64 usec_delta;
209
210 di->delta_tick = new_delta_tick;
211 if (lost_ticks) {
212 static int rate_limit;
213
214 /*
215 * We just lost some ticks. Just report it and don't
216 * try to adjust anything until we are stable again.
217 */
218 di->lost_ticks++;
219 di->lost_tick_count += abs(lost_ticks);
220 if ((rate_limit++ % 1003) == 0) {
221 /* FIXME: This should be a NOTICE.
222 * However we have several false ones at
223 * startup.
224 */
225 XBUS_DBG(SYNC, xbus, "Lost %d tick%s\n",
226 lost_ticks,
227 (abs(lost_ticks) > 1) ? "s" : "");
228 }
229 if (abs(lost_ticks) > 100) {
230 xbus_drift_clear(xbus);
231 ticker->count = ref_ticker->count;
232 }
233 } else {
234 /* Sample a delta */
235 usec_delta = ktime_us_delta(ticker->last_sample,
236 ref_ticker->last_sample);
237 sample_tick(xbus, usec_delta);
238 if ((ticker->count % SYNC_CYCLE) >
239 (SYNC_CYCLE - SYNC_CYCLE_SAMPLE))
240 di->delta_sum += usec_delta;
241
242 if ((ticker->count % SYNC_CYCLE) == 0) {
243 /*
244 * Full sampling cycle passed. Let's calculate
245 */
246 int offset =
247 di->delta_sum / SYNC_CYCLE_SAMPLE -
248 SYNC_CENTER;
249 int offset_prev = di->offset_prev;
250 int speed = xbus->sync_adjustment;
251 int fix = 0;
252 int best_speed =
253 (di->max_speed + di->min_speed) >> 1;
254
255 if (offset > 0 && offset < SYNC_DELTA) {
256 speed = best_speed - 1;
257 } else if (offset < 0 && offset > -SYNC_DELTA) {
258 speed = best_speed + 1;
259 } else {
260 if (offset > 0) {
261 if (offset > offset_prev)
262 fix--;
263 } else {
264 if (offset < offset_prev)
265 fix++;
266 }
267 speed += fix;
268 }
269 if (speed < -SYNC_ADJ_MAX)
270 speed = -SYNC_ADJ_MAX;
271 if (speed > SYNC_ADJ_MAX)
272 speed = SYNC_ADJ_MAX;
273 if (speed < di->min_speed)
274 di->min_speed = speed;
275 if (speed > di->max_speed)
276 di->max_speed = speed;
277 if (offset > di->offset_max)
278 di->offset_max = offset;
279 if (offset < di->offset_min)
280 di->offset_min = offset;
281
282 XBUS_DBG(SYNC, xbus,
283 "offset: %d, min_speed=%d, max_speed=%d, usec_delta(last)=%lld\n",
284 offset_prev, di->min_speed,
285 di->max_speed, usec_delta);
286 XBUS_DBG(SYNC, xbus,
287 "ADJ: speed=%d (best_speed=%d) fix=%d\n",
288 speed, best_speed, fix);
289 xbus->sync_adjustment_offset = speed;
290 if (xbus != syncer
291 && xbus->sync_adjustment != speed)
292 send_drift(xbus, speed);
293 di->sync_inaccuracy =
294 abs(offset) + abs(di->offset_range) / 2;
295 if (ticker->count >= SYNC_CYCLE * SYNC_CONVERGE) {
296 di->offset_range =
297 di->offset_max - di->offset_min;
298 di->offset_min = INT_MAX;
299 di->offset_max = -INT_MAX;
300 if (di->max_speed > best_speed)
301 di->max_speed--;
302 if (di->min_speed < best_speed)
303 di->min_speed++;
304 }
305 di->offset_prev = offset;
306 di->delta_sum = 0;
307 }
308 }
309 }
310 spin_unlock_irqrestore(&di->lock, flags);
311 }
312
313 const char *sync_mode_name(enum sync_mode mode)
314 {
315 static const char *sync_mode_names[] = {
316 [SYNC_MODE_AB] = "AB",
317 [SYNC_MODE_NONE] = "NONE",
318 [SYNC_MODE_PLL] = "PLL",
319 [SYNC_MODE_QUERY] = "QUERY",
320 };
321 if (mode >= ARRAY_SIZE(sync_mode_names))
322 return NULL;
323 return sync_mode_names[mode];
324 }
325 EXPORT_SYMBOL(sync_mode_name);
326
327 /*
328 * Caller must aquire/release the 'ref_ticker_lock' spinlock
329 */
330 static void xpp_set_syncer(xbus_t *xbus, bool on)
331 {
332 if (!xbus) { /* Special case, no more syncers */
333 DBG(SYNC, "No more syncers\n");
334 syncer = NULL;
335 if (ref_ticker != &dahdi_ticker)
336 ref_ticker = NULL;
337 return;
338 }
339 if (syncer != xbus && on) {
340 XBUS_DBG(SYNC, xbus, "New syncer\n");
341 syncer = xbus;
342 } else if (syncer == xbus && !on) {
343 XBUS_DBG(SYNC, xbus, "Lost syncer\n");
344 syncer = NULL;
345 if (ref_ticker != &dahdi_ticker)
346 ref_ticker = NULL;
347 } else
348 XBUS_DBG(SYNC, xbus, "ignore %s (current syncer: %s)\n",
349 (on) ? "ON" : "OFF",
350 (syncer) ? syncer->busname : "NO-SYNC");
351 }
352
353 static void xbus_command_timer(TIMER_DATA_TYPE timer)
354 {
355 xbus_t *xbus = from_timer(xbus, timer, command_timer);
356
357 BUG_ON(!xbus);
358 xbus_command_queue_tick(xbus);
359 if (!xbus->self_ticking) /* Must be 1KHz rate */
360 mod_timer(&xbus->command_timer, jiffies + 1);
361 }
362
363 void xbus_set_command_timer(xbus_t *xbus, bool on)
364 {
365 XBUS_DBG(SYNC, xbus, "%s\n", (on) ? "ON" : "OFF");
366 if (on) {
367 if (!timer_pending(&xbus->command_timer)) {
368 XBUS_DBG(SYNC, xbus, "add_timer\n");
369 timer_setup(&xbus->command_timer,
370 xbus_command_timer, 0);
371 mod_timer(&xbus->command_timer, jiffies + 1);
372 }
373 } else if (timer_pending(&xbus->command_timer)) {
374 XBUS_DBG(SYNC, xbus, "del_timer\n");
375 del_timer(&xbus->command_timer);
376 }
377 xbus->self_ticking = !on;
378 }
379
380 /*
381 * Called when the Astribank replies to a sync change request
382 */
383 void got_new_syncer(xbus_t *xbus, enum sync_mode mode, int drift)
384 {
385 unsigned long flags;
386 unsigned long flags2;
387
388 spin_lock_irqsave(&xbus->lock, flags);
389 spin_lock_irqsave(&ref_ticker_lock, flags2);
390 xbus->sync_adjustment = (signed char)drift;
391 if (xbus->sync_mode == mode) {
392 XBUS_DBG(SYNC, xbus, "Already in mode '%s'. Ignored\n",
393 sync_mode_name(mode));
394 goto out;
395 }
396 XBUS_DBG(SYNC, xbus, "Mode %s (%d), drift=%d (pcm_rx_counter=%d)\n",
397 sync_mode_name(mode), mode, drift,
398 atomic_read(&xbus->pcm_rx_counter));
399 switch (mode) {
400 case SYNC_MODE_AB:
401 xbus->sync_mode = mode;
402 xbus_set_command_timer(xbus, 0);
403 xpp_set_syncer(xbus, 1);
404 global_ticker = xbus;
405 break;
406 case SYNC_MODE_PLL:
407 xbus->sync_mode = mode;
408 xbus_set_command_timer(xbus, 0);
409 xpp_set_syncer(xbus, 0);
410 global_ticker = xbus;
411 break;
412 case SYNC_MODE_NONE: /* lost sync source */
413 xbus->sync_mode = mode;
414 xbus_set_command_timer(xbus, 1);
415 xpp_set_syncer(xbus, 0);
416 break;
417 case SYNC_MODE_QUERY: /* ignore */
418 break;
419 default:
420 XBUS_ERR(xbus, "%s: unknown mode=0x%X\n", __func__, mode);
421 }
422 out:
423 spin_unlock_irqrestore(&ref_ticker_lock, flags2);
424 spin_unlock_irqrestore(&xbus->lock, flags);
425 }
426 EXPORT_SYMBOL(got_new_syncer);
427
428 void xbus_request_sync(xbus_t *xbus, enum sync_mode mode)
429 {
430 unsigned long flags;
431
432 BUG_ON(!xbus);
433 XBUS_DBG(SYNC, xbus, "sent request (mode=%d)\n", mode);
434 CALL_PROTO(GLOBAL, SYNC_SOURCE, xbus, NULL, mode, 0);
435 if (mode == SYNC_MODE_NONE) {
436 /*
437 * We must deselect the syncer *now* and not wait for the
438 * reply from the AB. Otherwise, a disconnect of the syncing
439 * AB would result in a corrupted 'syncer'.
440 */
441 spin_lock_irqsave(&ref_ticker_lock, flags);
442 xpp_set_syncer(xbus, 0);
443 spin_unlock_irqrestore(&ref_ticker_lock, flags);
444 /*
445 * We must activate timer now since the commands go to
446 * the command queue, and we should maintain something to
447 * "tick" meanwhile until the AB get these commands and
448 * start being "self_ticking".
449 */
450 xbus_set_command_timer(xbus, 1);
451 }
452 }
453 EXPORT_SYMBOL(xbus_request_sync);
454
455 static void reset_sync_counters(void)
456 {
457 int i;
458
459 //DBG(SYNC, "%d\n", atomic_read(&xpp_tick_counter));
460 for (i = 0; i < MAX_BUSES; i++) {
461 xbus_t *xbus = get_xbus(__func__, i);
462
463 if (xbus == NULL)
464 continue;
465 /*
466 * Don't send to non self_ticking Astribanks:
467 * - Maybe they didn't finish initialization
468 * - Or maybe they didn't answer us in the first place
469 (e.g: wrong firmware version, etc).
470 */
471 if (xbus->self_ticking) {
472 if (!XBUS_FLAGS(xbus, CONNECTED)) {
473 XBUS_DBG(GENERAL, xbus,
474 "Dropped packet. Is shutting down.\n");
475 } else {
476 /* Reset sync LEDs once in a while */
477 CALL_PROTO(GLOBAL, RESET_SYNC_COUNTERS, xbus,
478 NULL);
479 }
480 }
481 put_xbus(__func__, xbus);
482 }
483 }
484
485 static void send_drift(xbus_t *xbus, int drift)
486 {
487 const ktime_t now = ktime_get();
488 const char *msg;
489 s64 msec_delta;
490
491 BUG_ON(abs(drift) > SYNC_ADJ_MAX);
492 if (drift > xbus->sync_adjustment)
493 msg = "up";
494 else
495 msg = "down";
496 msec_delta = ktime_ms_delta(now, xbus->pll_updated_at);
497 XBUS_DBG(SYNC, xbus,
498 "%sDRIFT adjust %s (%d) (last update %lld seconds ago)\n",
499 (disable_pll_sync) ? "Fake " : "", msg, drift,
500 msec_delta / MSEC_PER_SEC);
501 if (!disable_pll_sync)
502 CALL_PROTO(GLOBAL, SYNC_SOURCE, xbus, NULL, SYNC_MODE_PLL,
503 drift);
504 xbus->pll_updated_at = now;
505 }
506
507 static void global_tick(void)
508 {
509 ktime_t now = ktime_get();
510
511 atomic_inc(&xpp_tick_counter);
512 if ((atomic_read(&xpp_tick_counter) % BIG_TICK_INTERVAL) == 0)
513 reset_sync_counters();
514 xpp_ticker_step(&global_ticks_series, now);
515 }
516
517 #ifdef DAHDI_SYNC_TICK
518 void dahdi_sync_tick(struct dahdi_span *span, int is_master)
519 {
520 struct phonedev *phonedev = container_of(span, struct phonedev, span);
521 xpd_t *xpd = container_of(phonedev, struct xpd, phonedev);
522 static int redundant_ticks; /* for extra spans */
523 ktime_t now;
524
525 if (!force_dahdi_sync)
526 goto noop;
527 now = ktime_get();
528 BUG_ON(!xpd);
529 /*
530 * Detect if any of our spans is dahdi sync master
531 */
532 if (is_master) {
533 static int rate_limit;
534
535 if ((rate_limit++ % 10003) == 0)
536 XPD_NOTICE(xpd,
537 "Is a DAHDI sync master: "
538 "ignore sync from DAHDI\n");
539 goto noop;
540 }
541 /* Now we know for sure someone else is dahdi sync master */
542 if (syncer) {
543 static int rate_limit;
544
545 if ((rate_limit++ % 5003) == 0)
546 XBUS_DBG(SYNC, syncer,
547 "is a SYNCer: ignore sync from DAHDI\n");
548 goto noop;
549 }
550 /* ignore duplicate calls from all our registered spans */
551 if ((redundant_ticks++ % total_registered_spans()) != 0) {
552 #if 0
553 static int rate_limit;
554
555 if ((rate_limit++ % 1003) < 16)
556 XPD_NOTICE(xpd, "boop (%d)\n", dahdi_tick_count);
557 #endif
558 goto noop;
559 }
560 xpp_ticker_step(&dahdi_ticker, now);
561 dahdi_tick_count++;
562 //flip_parport_bit(1);
563 noop:
564 return;
565 }
566 EXPORT_SYMBOL(dahdi_sync_tick);
567 #endif
568
569 /*
570 * called from elect_syncer()
571 * if new_syncer is NULL, than we move all to SYNC_MODE_PLL
572 * for DAHDI sync.
573 */
574 static void update_sync_master(xbus_t *new_syncer, bool force_dahdi)
575 {
576 const char *msg;
577 int i;
578 unsigned long flags;
579
580 WARN_ON(new_syncer && force_dahdi); /* Ambigous */
581 force_dahdi_sync = force_dahdi;
582 msg = (force_dahdi_sync) ? "DAHDI" : "NO-SYNC";
583 DBG(SYNC, "%s => %s\n", (syncer) ? syncer->busname : msg,
584 (new_syncer) ? new_syncer->busname : msg);
585 /*
586 * This global locking protects:
587 * - The ref_ticker so it won't be used while we change it.
588 * - The xbus_drift_clear() from corrupting driftinfo data.
589 * It's important to set ref_ticker now:
590 * - We cannot make the new xbus a syncer yet (until we get
591 * a reply from AB). Maybe it's still not self_ticking, so
592 * we must keep the timer for the command_queue to function.
593 * - However, we must not send drift commands to it, because
594 * they'll revert it to PLL instead of AB.
595 */
596 spin_lock_irqsave(&ref_ticker_lock, flags);
597 if (syncer)
598 xbus_drift_clear(syncer); /* Clean old data */
599 if (new_syncer) {
600 XBUS_DBG(SYNC, new_syncer, "pcm_rx_counter=%d\n",
601 atomic_read(&new_syncer->pcm_rx_counter));
602 force_dahdi_sync = 0;
603 ref_ticker = &new_syncer->ticker;
604 xbus_drift_clear(new_syncer); /* Clean new data */
605 xpp_set_syncer(new_syncer, 1);
606 xbus_request_sync(new_syncer, SYNC_MODE_AB);
607 } else if (force_dahdi_sync) {
608 ref_ticker = &dahdi_ticker;
609 } else {
610 ref_ticker = NULL;
611 }
612 spin_unlock_irqrestore(&ref_ticker_lock, flags);
613 DBG(SYNC, "stop unwanted syncers\n");
614 /* Shut all down except the wanted sync master */
615 for (i = 0; i < MAX_BUSES; i++) {
616 xbus_t *xbus = get_xbus(__func__, i);
617 if (xbus == NULL)
618 continue;
619 if (XBUS_FLAGS(xbus, CONNECTED) && xbus != new_syncer) {
620 if (xbus->self_ticking)
621 xbus_request_sync(xbus,
622 xbus->sync_mode_default);
623 else
624 XBUS_DBG(SYNC, xbus,
625 "Not self_ticking yet. Ignore\n");
626 }
627 put_xbus(__func__, xbus);
628 }
629 }
630
631 void elect_syncer(const char *msg)
632 {
633 int i;
634 int j;
635 uint timing_priority = INT_MAX;
636 xpd_t *best_xpd = NULL;
637 xbus_t *the_xbus = NULL;
638 unsigned long flags;
639
640 spin_lock_irqsave(&elect_syncer_lock, flags);
641 DBG(SYNC, "%s: %s syncer=%s\n", __func__, msg,
642 (syncer) ? syncer->busname : "NULL");
643 for (i = 0; i < MAX_BUSES; i++) {
644 xbus_t *xbus = get_xbus(__func__, i);
645
646 if (xbus == NULL)
647 continue;
648 if (XBUS_IS(xbus, READY)) {
649 if (!the_xbus)
650 the_xbus = xbus; /* First candidate */
651 for (j = 0; j < MAX_XPDS; j++) {
652 xpd_t *xpd = xpd_of(xbus, j);
653 int prio;
654
655 if (!xpd || !xpd->card_present
656 || !IS_PHONEDEV(xpd))
657 continue;
658 prio =
659 CALL_PHONE_METHOD(card_timing_priority,
660 xpd);
661 if (prio < 0) {
662 DBG(SYNC, "%s/%s: skip sync\n",
663 xbus->busname, xpd->xpdname);
664 continue;
665 }
666 if (prio > 0 && prio < timing_priority) {
667 timing_priority = prio;
668 best_xpd = xpd;
669 }
670 }
671 }
672 put_xbus(__func__, xbus);
673 }
674 if (best_xpd) {
675 the_xbus = best_xpd->xbus;
676 XPD_DBG(SYNC, best_xpd, "%s: elected with priority %d\n", msg,
677 timing_priority);
678 } else if (the_xbus) {
679 XBUS_DBG(SYNC, the_xbus, "%s: elected\n", msg);
680 } else {
681 unsigned long flags;
682
683 DBG(SYNC, "%s: No more syncers\n", msg);
684 spin_lock_irqsave(&ref_ticker_lock, flags);
685 xpp_set_syncer(NULL, 0);
686 spin_unlock_irqrestore(&ref_ticker_lock, flags);
687 the_xbus = NULL;
688 }
689 if (the_xbus != syncer)
690 update_sync_master(the_xbus, force_dahdi_sync);
691 spin_unlock_irqrestore(&elect_syncer_lock, flags);
692 }
693 EXPORT_SYMBOL(elect_syncer);
694
695 /*
696 * This function should be called with the xpd already locked
697 */
698 void update_wanted_pcm_mask(xpd_t *xpd, xpp_line_t new_mask, uint new_pcm_len)
699 {
700 PHONEDEV(xpd).pcm_len = new_pcm_len;
701 PHONEDEV(xpd).wanted_pcm_mask = new_mask;
702 XPD_DBG(SIGNAL, xpd, "pcm_len=%d wanted_pcm_mask=0x%X\n",
703 PHONEDEV(xpd).pcm_len, PHONEDEV(xpd).wanted_pcm_mask);
704 }
705 EXPORT_SYMBOL(update_wanted_pcm_mask);
706
707 /*
708 * This function is used by FXS/FXO. The pcm_mask argument signifies
709 * channels which should be *added* to the automatic calculation.
710 * Normally, this argument is 0.
711 */
712 void generic_card_pcm_recompute(xpd_t *xpd, xpp_line_t pcm_mask)
713 {
714 int i;
715 int line_count = 0;
716 unsigned long flags;
717 uint pcm_len;
718
719 spin_lock_irqsave(&PHONEDEV(xpd).lock_recompute_pcm, flags);
720 //XPD_DBG(SIGNAL, xpd, "pcm_mask=0x%X\n", pcm_mask);
721 /* Add/remove all the trivial cases */
722 pcm_mask |= PHONEDEV(xpd).offhook_state;
723 pcm_mask |= PHONEDEV(xpd).oht_pcm_pass;
724 pcm_mask &= ~(PHONEDEV(xpd).digital_inputs);
725 pcm_mask &= ~(PHONEDEV(xpd).digital_outputs);
726 for_each_line(xpd, i)
727 if (IS_SET(pcm_mask, i))
728 line_count++;
729 /*
730 * FIXME: Workaround a bug in sync code of the Astribank.
731 * Send dummy PCM for sync.
732 */
733 if (xpd->addr.unit == 0 && pcm_mask == 0) {
734 pcm_mask = BIT(0);
735 line_count = 1;
736 }
737 pcm_len = (line_count)
738 ? RPACKET_HEADERSIZE + sizeof(xpp_line_t) +
739 line_count * DAHDI_CHUNKSIZE : 0L;
740 update_wanted_pcm_mask(xpd, pcm_mask, pcm_len);
741 spin_unlock_irqrestore(&PHONEDEV(xpd).lock_recompute_pcm, flags);
742 }
743 EXPORT_SYMBOL(generic_card_pcm_recompute);
744
745 void fill_beep(u_char *buf, int num, int duration)
746 {
747 bool alternate = (duration) ? (jiffies / (duration * 1000)) & 0x1 : 0;
748 int which;
749 u_char *snd;
750
751 /*
752 * debug tones
753 */
754 static u_char beep[] = {
755 0x7F, 0xBE, 0xD8, 0xBE, 0x80, 0x41, 0x24, 0x41, /* Dima */
756 0x67, 0x90, 0x89, 0x90, 0xFF, 0x10, 0x09, 0x10, /* Izzy */
757 };
758 static u_char beep_alt[] = {
759 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, /* silence */
760 };
761 if (alternate) {
762 which = num % ARRAY_SIZE(beep_alt);
763 snd = &beep_alt[which];
764 } else {
765 which = num % ARRAY_SIZE(beep);
766 snd = &beep[which];
767 }
768 memcpy(buf, snd, DAHDI_CHUNKSIZE);
769 }
770
771 static void do_ec(xpd_t *xpd)
772 {
773 int i;
774
775 for (i = 0; i < PHONEDEV(xpd).span.channels; i++) {
776 struct dahdi_chan *chan = XPD_CHAN(xpd, i);
777
778 /* Don't echo cancel BRI D-chans */
779 if (unlikely(IS_SET(PHONEDEV(xpd).digital_signalling, i)))
780 continue;
781 /* No ec for unwanted PCM */
782 if (!IS_SET(PHONEDEV(xpd).wanted_pcm_mask, i))
783 continue;
784 dahdi_ec_chunk(chan, chan->readchunk,
785 PHONEDEV(xpd).ec_chunk2[i]);
786 memcpy(PHONEDEV(xpd).ec_chunk2[i], PHONEDEV(xpd).ec_chunk1[i],
787 DAHDI_CHUNKSIZE);
788 memcpy(PHONEDEV(xpd).ec_chunk1[i], chan->writechunk,
789 DAHDI_CHUNKSIZE);
790 }
791 }
792
793 #if 0
794 /* Okay, now we get to the signalling. You have several options: */
795
796 /* Option 1: If you're a T1 like interface, you can just provide a
797 rbsbits function and we'll assert robbed bits for you. Be sure to
798 set the DAHDI_FLAG_RBS in this case. */
799
800 /* Opt: If the span uses A/B bits, set them here */
801 int (*rbsbits) (struct dahdi_chan * chan, int bits);
802
803 /* Option 2: If you don't know about sig bits, but do have their
804 equivalents (i.e. you can disconnect battery, detect off hook,
805 generate ring, etc directly) then you can just specify a
806 sethook function, and we'll call you with appropriate hook states
807 to set. Still set the DAHDI_FLAG_RBS in this case as well */
808 int (*hooksig) (struct dahdi_chan * chan, enum dahdi_txsig hookstate);
809
810 /* Option 3: If you can't use sig bits, you can write a function
811 which handles the individual hook states */
812 int (*sethook) (struct dahdi_chan * chan, int hookstate);
813 #endif
814
815 static bool pcm_valid(xpd_t *xpd, xpacket_t *pack)
816 {
817 xpp_line_t lines = RPACKET_FIELD(pack, GLOBAL, PCM_READ, lines);
818 int i;
819 int count = 0;
820 uint16_t good_len;
821
822 BUG_ON(!pack);
823 BUG_ON(XPACKET_OP(pack) != XPROTO_NAME(GLOBAL, PCM_READ));
824 /*
825 * Don't use for_each_line(xpd, i) here because for BRI it will
826 * ignore the channels of the other xpd's in the same unit.
827 */
828 for (i = 0; i < CHANNELS_PERXPD; i++)
829 if (IS_SET(lines, i))
830 count++;
831 /* FRAMES: include opcode in calculation */
832 good_len = RPACKET_HEADERSIZE + sizeof(xpp_line_t) + count * 8;
833 if (XPACKET_LEN(pack) != good_len) {
834 static int rate_limit;
835
836 XPD_COUNTER(xpd, RECV_ERRORS)++;
837 if ((rate_limit++ % 1000) <= 10) {
838 XPD_ERR(xpd,
839 "BAD PCM REPLY: packet_len=%d "
840 "(should be %d), count=%d\n",
841 XPACKET_LEN(pack), good_len, count);
842 dump_packet("BAD PCM REPLY", pack, 1);
843 }
844 return 0;
845 }
846 return 1;
847 }
848
849 static inline void pcm_frame_out(xbus_t *xbus, xframe_t *xframe)
850 {
851 unsigned long flags;
852 const ktime_t now = ktime_get();
853 s64 usec;
854
855 spin_lock_irqsave(&xbus->lock, flags);
856 if (unlikely(disable_pcm || !XBUS_IS(xbus, READY)))
857 goto dropit;
858 if (XPACKET_ADDR_SYNC((xpacket_t *)xframe->packets)) {
859 usec = ktime_us_delta(now, xbus->last_tx_sync);
860 xbus->last_tx_sync = now;
861 /* ignore startup statistics */
862 if (likely
863 (atomic_read(&xbus->pcm_rx_counter) > BIG_TICK_INTERVAL)) {
864 if (abs(usec - 1000) > TICK_TOLERANCE) {
865 static int rate_limit;
866
867 if ((rate_limit++ % 5003) == 0)
868 XBUS_DBG(SYNC, xbus,
869 "Bad PCM TX timing(%d): usec=%lld.\n",
870 rate_limit, usec);
871 }
872 if (usec > xbus->max_tx_sync)
873 xbus->max_tx_sync = usec;
874 if (usec < xbus->min_tx_sync)
875 xbus->min_tx_sync = usec;
876 }
877 }
878 spin_unlock_irqrestore(&xbus->lock, flags);
879 /* OK, really send it */
880 if (debug & DBG_PCM)
881 dump_xframe("TX_XFRAME_PCM", xbus, xframe, debug);
882 send_pcm_frame(xbus, xframe);
883 XBUS_COUNTER(xbus, TX_XFRAME_PCM)++;
884 return;
885 dropit:
886 spin_unlock_irqrestore(&xbus->lock, flags);
887 FREE_SEND_XFRAME(xbus, xframe);
888 }
889
890 /*
891 * Generic implementations of card_pcmfromspan()/card_pcmtospan()
892 * For FXS/FXO
893 */
894 void generic_card_pcm_fromspan(xpd_t *xpd, xpacket_t *pack)
895 {
896 __u8 *pcm;
897 unsigned long flags;
898 xpp_line_t wanted_lines;
899 int i;
900
901 BUG_ON(!xpd);
902 BUG_ON(!pack);
903 wanted_lines = PHONEDEV(xpd).wanted_pcm_mask;
904 RPACKET_FIELD(pack, GLOBAL, PCM_WRITE, lines) = wanted_lines;
905 pcm = RPACKET_FIELD(pack, GLOBAL, PCM_WRITE, pcm);
906 spin_lock_irqsave(&xpd->lock, flags);
907 for (i = 0; i < PHONEDEV(xpd).channels; i++) {
908 struct dahdi_chan *chan = XPD_CHAN(xpd, i);
909
910 if (IS_SET(wanted_lines, i)) {
911 if (SPAN_REGISTERED(xpd)) {
912 #ifdef DEBUG_PCMTX
913 int channo = chan->channo;
914
915 if (pcmtx >= 0 && pcmtx_chan == channo)
916 memset((u_char *)pcm, pcmtx,
917 DAHDI_CHUNKSIZE);
918 else
919 #endif
920 memcpy((u_char *)pcm, chan->writechunk,
921 DAHDI_CHUNKSIZE);
922 } else
923 memset((u_char *)pcm, 0x7F, DAHDI_CHUNKSIZE);
924 pcm += DAHDI_CHUNKSIZE;
925 }
926 }
927 XPD_COUNTER(xpd, PCM_WRITE)++;
928 spin_unlock_irqrestore(&xpd->lock, flags);
929 }
930 EXPORT_SYMBOL(generic_card_pcm_fromspan);
931
932 void generic_card_pcm_tospan(xpd_t *xpd, xpacket_t *pack)
933 {
934 __u8 *pcm;
935 xpp_line_t pcm_mask;
936 xpp_line_t pcm_mute;
937 unsigned long flags;
938 int i;
939
940 pcm = RPACKET_FIELD(pack, GLOBAL, PCM_READ, pcm);
941 pcm_mask = RPACKET_FIELD(pack, GLOBAL, PCM_READ, lines);
942 spin_lock_irqsave(&xpd->lock, flags);
943 /*
944 * Calculate the channels we want to mute
945 */
946 pcm_mute = ~(PHONEDEV(xpd).wanted_pcm_mask);
947 pcm_mute |= PHONEDEV(xpd).mute_dtmf | PHONEDEV(xpd).silence_pcm;
948 if (!SPAN_REGISTERED(xpd))
949 goto out;
950 for (i = 0; i < PHONEDEV(xpd).channels; i++) {
951 volatile u_char *r = XPD_CHAN(xpd, i)->readchunk;
952 bool got_data = IS_SET(pcm_mask, i);
953
954 if (got_data && !IS_SET(pcm_mute, i)) {
955 /* We have and want real data */
956 // memset((u_char *)r, 0x5A, DAHDI_CHUNKSIZE); // DEBUG
957 memcpy((u_char *)r, pcm, DAHDI_CHUNKSIZE);
958 } else
959 if (IS_SET
960 (PHONEDEV(xpd).wanted_pcm_mask | PHONEDEV(xpd).
961 silence_pcm, i)) {
962 /* Inject SILENCE */
963 memset((u_char *)r, 0x7F, DAHDI_CHUNKSIZE);
964 if (IS_SET(PHONEDEV(xpd).silence_pcm, i)) {
965 /*
966 * This will clear the EC buffers until next
967 * tick so we don't have noise residues
968 * from the past.
969 */
970 memset(PHONEDEV(xpd).ec_chunk2[i], 0x7F,
971 DAHDI_CHUNKSIZE);
972 memset(PHONEDEV(xpd).ec_chunk1[i], 0x7F,
973 DAHDI_CHUNKSIZE);
974 }
975 }
976 if (got_data)
977 pcm += DAHDI_CHUNKSIZE;
978 }
979 out:
980 XPD_COUNTER(xpd, PCM_READ)++;
981 spin_unlock_irqrestore(&xpd->lock, flags);
982 }
983 EXPORT_SYMBOL(generic_card_pcm_tospan);
984
985 int generic_echocancel_timeslot(xpd_t *xpd, int pos)
986 {
987 return xpd->addr.unit * 32 + pos;
988 }
989 EXPORT_SYMBOL(generic_echocancel_timeslot);
990
991 int generic_echocancel_setmask(xpd_t *xpd, xpp_line_t ec_mask)
992 {
993 int i;
994
995 BUG_ON(!xpd);
996 XPD_DBG(GENERAL, xpd, "0x%8X\n", ec_mask);
997 if (!ECHOOPS(xpd->xbus)) {
998 XPD_DBG(GENERAL, xpd,
999 "No echo canceller in XBUS: Doing nothing.\n");
1000 return -EINVAL;
1001 }
1002 for (i = 0; i < PHONEDEV(xpd).channels; i++) {
1003 int on = BIT(i) & ec_mask;
1004 CALL_EC_METHOD(ec_set, xpd->xbus, xpd, i, on);
1005 }
1006 CALL_EC_METHOD(ec_update, xpd->xbus, xpd->xbus);
1007 return 0;
1008 }
1009 EXPORT_SYMBOL(generic_echocancel_setmask);
1010
1011 static int copy_pcm_tospan(xbus_t *xbus, xframe_t *xframe)
1012 {
1013 __u8 *xframe_end;
1014 xpacket_t *pack;
1015 __u8 *p;
1016 int ret = -EPROTO; /* Assume error */
1017
1018 if (debug & DBG_PCM)
1019 dump_xframe("RX_XFRAME_PCM", xbus, xframe, debug);
1020 /* handle content */
1021
1022 p = xframe->packets;
1023 xframe_end = p + XFRAME_LEN(xframe);
1024 do {
1025 int len;
1026 xpd_t *xpd;
1027
1028 pack = (xpacket_t *)p;
1029 len = XPACKET_LEN(pack);
1030 /* Sanity checks */
1031 if (unlikely(XPACKET_OP(pack) != XPROTO_NAME(GLOBAL, PCM_READ))) {
1032 static int rate_limit;
1033
1034 if ((rate_limit++ % 1003) == 0) {
1035 XBUS_NOTICE(xbus,
1036 "%s: Non-PCM packet within "
1037 "a PCM xframe. (%d)\n",
1038 __func__, rate_limit);
1039 dump_xframe("In PCM xframe", xbus, xframe,
1040 debug);
1041 }
1042 goto out;
1043 }
1044 p += len;
1045 if (p > xframe_end || len < RPACKET_HEADERSIZE) {
1046 static int rate_limit;
1047
1048 if ((rate_limit++ % 1003) == 0) {
1049 XBUS_NOTICE(xbus,
1050 "%s: Invalid packet length %d. (%d)\n",
1051 __func__, len, rate_limit);
1052 dump_xframe("BAD LENGTH", xbus, xframe, debug);
1053 }
1054 goto out;
1055 }
1056 xpd =
1057 xpd_byaddr(xbus, XPACKET_ADDR_UNIT(pack),
1058 XPACKET_ADDR_SUBUNIT(pack));
1059 if (unlikely(!xpd)) {
1060 static int rate_limit;
1061
1062 if ((rate_limit++ % 1003) == 0) {
1063 notify_bad_xpd(__func__, xbus,
1064 XPACKET_ADDR(pack),
1065 "RECEIVE PCM");
1066 dump_xframe("Unknown XPD addr", xbus, xframe,
1067 debug);
1068 }
1069 goto out;
1070 }
1071 if (!pcm_valid(xpd, pack))
1072 goto out;
1073 if (SPAN_REGISTERED(xpd)) {
1074 XBUS_COUNTER(xbus, RX_PACK_PCM)++;
1075 CALL_PHONE_METHOD(card_pcm_tospan, xpd, pack);
1076 }
1077 } while (p < xframe_end);
1078 ret = 0; /* all good */
1079 XBUS_COUNTER(xbus, RX_XFRAME_PCM)++;
1080 out:
1081 FREE_RECV_XFRAME(xbus, xframe);
1082 return ret;
1083 }
1084
1085 int generic_timing_priority(xpd_t *xpd)
1086 {
1087 return PHONEDEV(xpd).timing_priority;
1088 }
1089 EXPORT_SYMBOL(generic_timing_priority);
1090
1091 static void xbus_tick(xbus_t *xbus)
1092 {
1093 int i;
1094 xpd_t *xpd;
1095 xframe_t *xframe = NULL;
1096 xpacket_t *pack = NULL;
1097 bool sent_sync_bit = 0;
1098
1099 /*
1100 * Update dahdi
1101 */
1102 for (i = 0; i < MAX_XPDS; i++) {
1103 xpd = xpd_of(xbus, i);
1104 if (xpd && SPAN_REGISTERED(xpd)) {
1105 #ifdef OPTIMIZE_CHANMUTE
1106 int j;
1107 xpp_line_t xmit_mask = PHONEDEV(xpd).wanted_pcm_mask;
1108
1109 xmit_mask |= PHONEDEV(xpd).silence_pcm;
1110 xmit_mask |= PHONEDEV(xpd).digital_signalling;
1111 for_each_line(xpd, j) {
1112 XPD_CHAN(xpd, j)->chanmute = (optimize_chanmute)
1113 ? !IS_SET(xmit_mask, j)
1114 : 0;
1115 }
1116 #endif
1117 /*
1118 * calls to dahdi_transmit should be out of spinlocks,
1119 * as it may call back our hook setting methods.
1120 */
1121 dahdi_transmit(&PHONEDEV(xpd).span);
1122 }
1123 }
1124 /*
1125 * Fill xframes
1126 */
1127 for (i = 0; i < MAX_XPDS; i++) {
1128 if ((xpd = xpd_of(xbus, i)) == NULL)
1129 continue;
1130 if (!IS_PHONEDEV(xpd))
1131 continue;
1132 if (SPAN_REGISTERED(xpd)) {
1133 size_t pcm_len = PHONEDEV(xpd).pcm_len;
1134
1135 if (pcm_len && xpd->card_present) {
1136 do {
1137 #if 0
1138 /* FORCE single packet frames */
1139 pack = NULL;
1140 #endif
1141 if (xframe && !pack) { /* FULL frame */
1142 pcm_frame_out(xbus, xframe);
1143 xframe = NULL;
1144 XBUS_COUNTER(xbus,
1145 TX_PCM_FRAG)++;
1146 }
1147 if (!xframe) { /* Alloc frame */
1148 xframe =
1149 ALLOC_SEND_XFRAME(xbus);
1150 if (!xframe) {
1151 static int rate_limit;
1152
1153 if ((rate_limit++ % 3001) == 0)
1154 XBUS_ERR(xbus,
1155 "%s: failed to allocate new xframe\n",
1156 __func__);
1157 return;
1158 }
1159 }
1160 pack =
1161 xframe_next_packet(xframe, pcm_len);
1162 } while (!pack);
1163 XPACKET_INIT(pack, GLOBAL, PCM_WRITE,
1164 xpd->xbus_idx, 1, 0);
1165 XPACKET_LEN(pack) = pcm_len;
1166 if (!sent_sync_bit) {
1167 XPACKET_ADDR_SYNC(pack) = 1;
1168 sent_sync_bit = 1;
1169 }
1170 CALL_PHONE_METHOD(card_pcm_fromspan, xpd, pack);
1171 XBUS_COUNTER(xbus, TX_PACK_PCM)++;
1172 }
1173 }
1174 }
1175 if (xframe) /* clean any leftovers */
1176 pcm_frame_out(xbus, xframe);
1177 /*
1178 * Receive PCM
1179 */
1180 while ((xframe = xframe_dequeue(&xbus->pcm_tospan)) != NULL) {
1181 copy_pcm_tospan(xbus, xframe);
1182 if (XPACKET_ADDR_SYNC((xpacket_t *)xframe->packets)) {
1183 ktime_t now = ktime_get();
1184 s64 usec;
1185
1186 usec = ktime_us_delta(now, xbus->last_rx_sync);
1187 xbus->last_rx_sync = now;
1188 /* ignore startup statistics */
1189 if (likely
1190 (atomic_read(&xbus->pcm_rx_counter) >
1191 BIG_TICK_INTERVAL)) {
1192 if (abs(usec - 1000) > TICK_TOLERANCE) {
1193 static int rate_limit;
1194
1195 if ((rate_limit++ % 5003) == 0)
1196 XBUS_DBG(SYNC, xbus,
1197 "Bad PCM RX timing(%d): usec=%lld.\n",
1198 rate_limit, usec);
1199 }
1200 if (usec > xbus->max_rx_sync)
1201 xbus->max_rx_sync = usec;
1202 if (usec < xbus->min_rx_sync)
1203 xbus->min_rx_sync = usec;
1204 }
1205 }
1206 }
1207 for (i = 0; i < MAX_XPDS; i++) {
1208 xpd = xpd_of(xbus, i);
1209 if (!xpd || !xpd->card_present)
1210 continue;
1211 if (IS_PHONEDEV(xpd)) {
1212 if (SPAN_REGISTERED(xpd)) {
1213 do_ec(xpd);
1214 dahdi_receive(&PHONEDEV(xpd).span);
1215 }
1216 /* silence was injected */
1217 PHONEDEV(xpd).silence_pcm = 0;
1218 }
1219 xpd->timer_count = xbus->global_counter;
1220 /*
1221 * Must be called *after* tx/rx so
1222 * D-Chan counters may be cleared
1223 */
1224 CALL_XMETHOD(card_tick, xpd);
1225 }
1226 }
1227
1228 static void do_tick(xbus_t *xbus, const ktime_t kt_received)
1229 {
1230 int counter = atomic_read(&xpp_tick_counter);
1231 unsigned long flags;
1232
1233 xbus_command_queue_tick(xbus);
1234 if (global_ticker == xbus)
1235 global_tick(); /* called from here or dahdi_sync_tick() */
1236 spin_lock_irqsave(&ref_ticker_lock, flags);
1237 xpp_drift_step(xbus, kt_received);
1238 spin_unlock_irqrestore(&ref_ticker_lock, flags);
1239 if (likely(xbus->self_ticking))
1240 xbus_tick(xbus);
1241 xbus->global_counter = counter;
1242 }
1243
1244 void xframe_receive_pcm(xbus_t *xbus, xframe_t *xframe)
1245 {
1246 if (!xframe_enqueue(&xbus->pcm_tospan, xframe)) {
1247 static int rate_limit;
1248
1249 if ((rate_limit++ % 1003) == 0)
1250 XBUS_DBG(SYNC, xbus,
1251 "Failed to enqueue received pcm frame. (%d)\n",
1252 rate_limit);
1253 FREE_RECV_XFRAME(xbus, xframe);
1254 }
1255 /*
1256 * The sync_master bit is marked at the first packet
1257 * of the frame, regardless of the XPD that is sync master.
1258 * FIXME: what about PRI split?
1259 */
1260 if (XPACKET_ADDR_SYNC((xpacket_t *)xframe->packets)) {
1261 do_tick(xbus, xframe->kt_received);
1262 atomic_inc(&xbus->pcm_rx_counter);
1263 } else
1264 xbus->xbus_frag_count++;
1265 }
1266
1267 int exec_sync_command(const char *buf, size_t count)
1268 {
1269 int ret = count;
1270 int xbusno;
1271 xbus_t *xbus;
1272
1273 if (strncmp("DAHDI", buf, 6) == 0) { /* Ignore the newline */
1274 DBG(SYNC, "DAHDI");
1275 update_sync_master(NULL, 1);
1276 } else if (sscanf(buf, "SYNC=%d", &xbusno) == 1) {
1277 DBG(SYNC, "SYNC=%d\n", xbusno);
1278 xbus = get_xbus(__func__, xbusno);
1279 if (xbus == NULL) {
1280 ERR("No bus %d exists\n", xbusno);
1281 return -ENXIO;
1282 }
1283 update_sync_master(xbus, 0);
1284 put_xbus(__func__, xbus);
1285 } else if (sscanf(buf, "QUERY=%d", &xbusno) == 1) {
1286 DBG(SYNC, "QUERY=%d\n", xbusno);
1287 xbus = get_xbus(__func__, xbusno);
1288 if (xbus == NULL) {
1289 ERR("No bus %d exists\n", xbusno);
1290 return -ENXIO;
1291 }
1292 CALL_PROTO(GLOBAL, SYNC_SOURCE, xbus, NULL, SYNC_MODE_QUERY, 0);
1293 put_xbus(__func__, xbus);
1294 } else {
1295 ERR("%s: cannot parse '%s'\n", __func__, buf);
1296 ret = -EINVAL;
1297 }
1298 return ret;
1299 }
1300
1301 int fill_sync_string(char *buf, size_t count)
1302 {
1303 int len = 0;
1304
1305 if (!syncer) {
1306 len +=
1307 snprintf(buf, count, "%s\n",
1308 (force_dahdi_sync) ? "DAHDI" : "NO-SYNC");
1309 } else
1310 len += snprintf(buf, count, "SYNC=%02d\n", syncer->num);
1311 return len;
1312 }
1313
1314 int xbus_pcm_init(void *toplevel)
1315 {
1316 int ret = 0;
1317
1318 #ifdef OPTIMIZE_CHANMUTE
1319 INFO("FEATURE: with CHANMUTE optimization (%sactivated)\n",
1320 (optimize_chanmute) ? "" : "de");
1321 #endif
1322 #ifdef DAHDI_SYNC_TICK
1323 INFO("FEATURE: with sync_tick() from DAHDI\n");
1324 #else
1325 INFO("FEATURE: without sync_tick() from DAHDI\n");
1326 #endif
1327 xpp_ticker_init(&global_ticks_series);
1328 xpp_ticker_init(&dahdi_ticker);
1329 return ret;
1330 }
1331
1332 void xbus_pcm_shutdown(void)
1333 {
1334 }