Debian Codegraph @

drivers/dahdi/xpp/xbus-pcm.c

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