Debian Codegraph @

include/dahdi/kernel.h

raw
1 /*
2 * DAHDI Telephony Interface
3 *
4 * Written by Mark Spencer <[email protected]>
5 * Based on previous works, designs, and architectures conceived and
6 * written by Jim Dixon <[email protected]>.
7 *
8 * Copyright (C) 2001 Jim Dixon / Zapata Telephony.
9 * Copyright (C) 2001 - 2012 Digium, Inc.
10 *
11 * All rights reserved.
12 *
13 */
14
15 /*
16 * See http://www.asterisk.org for more information about
17 * the Asterisk project. Please do not directly contact
18 * any of the maintainers of this project for assistance;
19 * the project provides a web site, mailing lists and IRC
20 * channels for your use.
21 *
22 * This program is free software, distributed under the terms of
23 * the GNU General Public License Version 2 as published by the
24 * Free Software Foundation. See the LICENSE file included with
25 * this program for more details.
26 */
27
28 /*!
29 * \file
30 * \brief DAHDI kernel interface definitions
31 */
32
33 #ifndef _DAHDI_KERNEL_H
34 #define _DAHDI_KERNEL_H
35
36 #include <dahdi/user.h>
37 #include <dahdi/fasthdlc.h>
38
39 #include <dahdi/dahdi_config.h>
40 #include <linux/version.h>
41 #include <linux/fs.h>
42 #include <linux/device.h>
43 #include <linux/cdev.h>
44 #include <linux/module.h>
45 #include <linux/ioctl.h>
46
47 #ifdef CONFIG_DAHDI_NET
48 #include <linux/hdlc.h>
49 #endif
50
51 #ifdef CONFIG_DAHDI_PPP
52 #include <linux/ppp_channel.h>
53 #include <linux/skbuff.h>
54 #include <linux/interrupt.h>
55 #endif
56 #include <linux/device.h>
57 #include <linux/sysfs.h>
58
59 #include <linux/poll.h>
60
61 #define dahdi_pci_module pci_register_driver
62
63 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
64 #define DAHDI_IRQ_HANDLER(a) static irqreturn_t a(int irq, void *dev_id)
65 #else
66 #define DAHDI_IRQ_HANDLER(a) static irqreturn_t a(int irq, void *dev_id, struct pt_regs *regs)
67 #endif
68
69 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)
70 #ifdef CONFIG_PCI
71 #include <linux/pci-aspm.h>
72 #endif
73 #endif
74
75 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
76 #define HAVE_NET_DEVICE_OPS
77 #endif
78
79 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
80 # ifdef RHEL_RELEASE_VERSION
81 # if RHEL_RELEASE_CODE < RHEL_RELEASE_VERSION(5, 6)
82 #define dev_name(dev) ((dev)->bus_id)
83 #define dev_set_name(dev, format, ...) \
84 snprintf((dev)->bus_id, BUS_ID_SIZE, format, ## __VA_ARGS__)
85 # else
86 #define dev_set_name(dev, format, ...) \
87 do { \
88 kobject_set_name(&(dev)->kobj, format, ## __VA_ARGS__); \
89 snprintf((dev)->bus_id, BUS_ID_SIZE, \
90 kobject_name(&(dev)->kobj)); \
91 } while (0)
92 # endif
93 # else
94 #define dev_name(dev) ((dev)->bus_id)
95 #define dev_set_name(dev, format, ...) \
96 snprintf((dev)->bus_id, BUS_ID_SIZE, format, ## __VA_ARGS__)
97 # endif
98 #endif
99
100 /* __dev* were removed in 3.8. They still have effect in 2.6.18. */
101 #ifndef __devinit
102 # define __devinit
103 # define __devinitdata
104 # define __devexit
105 # define __devexit_p(x) x
106 #endif
107
108 /*! Default chunk size for conferences and such -- static right now, might make
109 variable sometime. 8 samples = 1 ms = most frequent service interval possible
110 for a USB device */
111 #define DAHDI_CHUNKSIZE 8
112 #define DAHDI_MIN_CHUNKSIZE DAHDI_CHUNKSIZE
113 #define DAHDI_DEFAULT_CHUNKSIZE DAHDI_CHUNKSIZE
114 #define DAHDI_MAX_CHUNKSIZE DAHDI_CHUNKSIZE
115 #define DAHDI_CB_SIZE (1 << 3)
116
117 /* DAHDI operates at 8Khz by default */
118 #define DAHDI_MS_TO_SAMPLES(ms) ((ms) * 8)
119
120 #define DAHDI_MSECS_PER_CHUNK (DAHDI_CHUNKSIZE/DAHDI_MS_TO_SAMPLES(1))
121
122 #define RING_DEBOUNCE_TIME 2000 /*!< 2000 ms ring debounce time */
123
124 typedef struct
125 {
126 int32_t gain;
127 int32_t a1;
128 int32_t a2;
129 int32_t b1;
130 int32_t b2;
131
132 int32_t z1;
133 int32_t z2;
134 } biquad2_state_t;
135
136 typedef struct
137 {
138 biquad2_state_t notch;
139 int notch_level;
140 int channel_level;
141 int tone_present;
142 int tone_cycle_duration;
143 int good_cycles;
144 int hit;
145 } echo_can_disable_detector_state_t;
146
147 struct sf_detect_state {
148 long x1;
149 long x2;
150 long y1;
151 long y2;
152 long e1;
153 long e2;
154 int samps;
155 int lastdetect;
156 };
157
158 struct dahdi_tone_state {
159 int v1_1;
160 int v2_1;
161 int v3_1;
162 int v1_2;
163 int v2_2;
164 int v3_2;
165 int modulate;
166 };
167
168 /*! \brief Conference queue structure */
169 struct confq {
170 u_char buffer[DAHDI_CHUNKSIZE * DAHDI_CB_SIZE];
171 u_char *buf[DAHDI_CB_SIZE];
172 int inbuf;
173 int outbuf;
174 };
175
176 struct dahdi_chan;
177 struct dahdi_echocan_state;
178
179 /*! Features a DAHDI echo canceler (software or hardware) can provide to the DAHDI core. */
180 struct dahdi_echocan_features {
181
182 /*! Able to detect CED tone (2100 Hz with phase reversals) in the transmit direction.
183 * If the echocan can detect this tone, it may report it it as an event (see
184 * the events.CED_tx_detected field of dahdi_echocan_state), and if it will automatically
185 * disable itself or its non-linear processor, then the NLP_automatic feature flag should also
186 * be set so that the DAHDI core doesn't bother trying to do so.
187 */
188 u32 CED_tx_detect:1;
189
190 /*! Able to detect CED tone (2100 Hz with phase reversals) in the receive direction.
191 * If the echocan can detect this tone, it may report it it as an event (see
192 * the events.CED_rx_detected field of dahdi_echocan_state), and if it will automatically
193 * disable itself or its non-linear processor, then the NLP_automatic flag feature should also
194 * be set so that the DAHDI core doesn't bother trying to do so.
195 */
196 u32 CED_rx_detect:1;
197
198 /*! Able to detect CNG tone (1100 Hz) in the transmit direction. */
199 u32 CNG_tx_detect:1;
200
201 /*! Able to detect CNG tone (1100 Hz) in the receive direction. */
202 u32 CNG_rx_detect:1;
203
204 /*! If the echocan's NLP can be enabled and disabled without requiring destruction
205 * and recreation of the state structure, this feature flag should be set and the
206 * echocan_NLP_toggle field of the dahdi_echocan_ops structure should be filled with a
207 * pointer to the function to perform that operation.
208 */
209 u32 NLP_toggle:1;
210
211 /*! If the echocan will automatically disable itself (or even just its NLP) based on
212 * detection of a CED tone in either direction, this feature flag should be set (along
213 * with the tone detection feature flags).
214 */
215 u32 NLP_automatic:1;
216 };
217
218 /*! Operations (methods) that can be performed on a DAHDI echo canceler instance (state
219 * structure) after it has been created, by either a software or hardware echo canceller.
220 * The echo canceler must populate the owner field of the dahdi_echocan_state structure
221 * with a pointer to the relevant operations structure for that instance.
222 */
223 struct dahdi_echocan_ops {
224
225 /*! \brief Free an echocan state structure.
226 * \param[in,out] ec Pointer to the state structure to free.
227 *
228 * \return Nothing.
229 */
230 void (*echocan_free)(struct dahdi_chan *chan, struct dahdi_echocan_state *ec);
231
232 /*! \brief Process an array of audio samples through the echocan.
233 * \param[in,out] ec Pointer to the state structure.
234 * \param[in,out] isig The receive direction data (will be modified).
235 * \param[in] iref The transmit direction data.
236 * \param[in] size The number of elements in the isig and iref arrays.
237 *
238 * Note: This function can also return events in the events field of the
239 * dahdi_echocan_state structure. If it can do so, then the echocan does
240 * not need to provide the echocan_events function.
241 *
242 * \return Nothing.
243 */
244 void (*echocan_process)(struct dahdi_echocan_state *ec, short *isig, const short *iref, u32 size);
245
246 /*! \brief Retrieve events from the echocan.
247 * \param[in,out] ec Pointer to the state structure.
248 *
249 *
250 * If any events have occurred, the events field of the dahdi_echocan_state
251 * structure should be updated to include them.
252 *
253 * \return Nothing.
254 */
255 void (*echocan_events)(struct dahdi_echocan_state *ec);
256
257 /*! \brief Feed a sample (and its position) for echocan training.
258 * \param[in,out] ec Pointer to the state structure.
259 * \param[in] pos The tap position to be 'trained'.
260 * \param[in] val The receive direction sample for the specified tap position.
261 *
262 * \retval Zero if training should continue.
263 * \retval Non-zero if training is complete.
264 */
265 int (*echocan_traintap)(struct dahdi_echocan_state *ec, int pos, short val);
266
267 /*! \brief Enable or disable non-linear processing (NLP) in the echocan.
268 * \param[in,out] ec Pointer to the state structure.
269 * \param[in] enable Zero to disable, non-zero to enable.
270 *
271 * \return Nothing.
272 */
273 void (*echocan_NLP_toggle)(struct dahdi_echocan_state *ec, unsigned int enable);
274
275 #ifdef CONFIG_DAHDI_ECHOCAN_PROCESS_TX
276 /*! \brief Process an array of TX audio samples.
277 *
278 * \return Nothing.
279 */
280 void (*echocan_process_tx)(struct dahdi_echocan_state *ec,
281 short *tx, u32 size);
282 #endif
283 };
284
285 /*! A factory for creating instances of software echo cancelers to be used on DAHDI channels. */
286 struct dahdi_echocan_factory {
287
288 /*! Get the name of the factory. */
289 const char *(*get_name)(const struct dahdi_chan *chan);
290
291 /*! Pointer to the module that owns this factory; the module's reference count will be
292 * incremented/decremented by the DAHDI core as needed.
293 */
294 struct module *owner;
295
296 /*! \brief Function to create an instance of the echocan.
297 * \param[in] ecp Structure defining parameters to be used for the instance creation.
298 * \param[in] p Pointer to the beginning of an (optional) array of user-defined parameters.
299 * \param[out] ec Pointer to the state structure that is created, if any.
300 *
301 * \retval Zero on success.
302 * \retval Non-zero on failure (return value will be returned to userspace so it should be a
303 * standard error number).
304 */
305 int (*echocan_create)(struct dahdi_chan *chan, struct dahdi_echocanparams *ecp,
306 struct dahdi_echocanparam *p, struct dahdi_echocan_state **ec);
307 };
308
309 /*! \brief Register an echo canceler factory with the DAHDI core.
310 * \param[in] ec Pointer to the dahdi_echocan_factory structure to be registered.
311 *
312 * \retval Zero on success.
313 * \retval Non-zero on failure (return value will be a standard error number).
314 */
315 int dahdi_register_echocan_factory(const struct dahdi_echocan_factory *ec);
316
317 /*! \brief Unregister a previously-registered echo canceler factory from the DAHDI core.
318 * \param[in] ec Pointer to the dahdi_echocan_factory structure to be unregistered.
319 *
320 * \return Nothing.
321 */
322 void dahdi_unregister_echocan_factory(const struct dahdi_echocan_factory *ec);
323
324 enum dahdi_echocan_mode {
325 __ECHO_MODE_MUTE = 1 << 8,
326 ECHO_MODE_IDLE = 0,
327 ECHO_MODE_PRETRAINING = 1 | __ECHO_MODE_MUTE,
328 ECHO_MODE_STARTTRAINING = 2 | __ECHO_MODE_MUTE,
329 ECHO_MODE_AWAITINGECHO = 3 | __ECHO_MODE_MUTE,
330 ECHO_MODE_TRAINING = 4 | __ECHO_MODE_MUTE,
331 ECHO_MODE_ACTIVE = 5,
332 ECHO_MODE_FAX = 6,
333 };
334
335 /*! An instance of a DAHDI echo canceler (software or hardware). */
336 struct dahdi_echocan_state {
337
338 /*! Pointer to a dahdi_echocan_ops structure of operations that can be
339 * performed on this instance.
340 */
341 const struct dahdi_echocan_ops *ops;
342
343 /*! State data used by the DAHDI core's CED detector for the transmit
344 * direction, if needed.
345 */
346 echo_can_disable_detector_state_t txecdis;
347
348 /*! State data used by the DAHDI core's CED detector for the receive
349 * direction, if needed.
350 */
351 echo_can_disable_detector_state_t rxecdis;
352
353 /*! Features offered by the echo canceler that provided this instance. */
354 struct dahdi_echocan_features features;
355
356 struct {
357 /*! The mode the echocan is currently in. */
358 enum dahdi_echocan_mode mode;
359
360 /*! The last tap position that was fed to the echocan's training function. */
361 u32 last_train_tap;
362
363 /*! How many samples to wait before beginning the training operation. */
364 u32 pretrain_timer;
365 } status;
366
367 /*! This structure contains event flags, allowing the echocan to report
368 * events that occurred as it processed the transmit and receive streams
369 * of samples. Each call to the echocan_process operation for this
370 * instance may report events, so the structure should be cleared before
371 * calling that operation.
372 */
373 union dahdi_echocan_events {
374 u32 all;
375 struct {
376 /*! CED tone was detected in the transmit direction. If the
377 * echocan automatically disables its NLP when this occurs,
378 * it must also signal the NLP_auto_disabled event during the *same*
379 * call to echocan_process that reports the CED detection.
380 */
381 u32 CED_tx_detected:1;
382
383 /*! CED tone was detected in the receive direction. If the
384 * echocan automatically disables its NLP when this occurs,
385 * it must also signal the NLP_auto_disabled event during the *same*
386 * call to echocan_process that reports the CED detection.
387 */
388 u32 CED_rx_detected:1;
389
390 /*! CNG tone was detected in the transmit direction. */
391 u32 CNG_tx_detected:1;
392
393 /*! CNG tone was detected in the receive direction. */
394 u32 CNG_rx_detected:1;
395
396 /*! The echocan disabled its NLP automatically.
397 */
398 u32 NLP_auto_disabled:1;
399
400 /*! The echocan enabled its NLP automatically.
401 */
402 u32 NLP_auto_enabled:1;
403 } bit;
404 } events;
405 };
406
407 struct dahdi_chan {
408 #ifdef CONFIG_DAHDI_NET
409 /*! \note Must be first */
410 struct dahdi_hdlc *hdlcnetdev;
411 #endif
412 #ifdef CONFIG_DAHDI_PPP
413 struct ppp_channel *ppp;
414 struct tasklet_struct ppp_calls;
415 int do_ppp_wakeup;
416 int do_ppp_error;
417 struct sk_buff_head ppp_rq;
418 #endif
419 #ifdef BUFFER_DEBUG
420 int statcount;
421 int lastnumbufs;
422 #endif
423 spinlock_t lock;
424 struct mutex mutex;
425 char name[40];
426 /* Specified by DAHDI */
427 /*! \brief DAHDI channel number */
428 int channo;
429 int chanpos;
430 unsigned long flags;
431 long rxp1;
432 long rxp2;
433 long rxp3;
434 int txtone;
435 int tx_v2;
436 int tx_v3;
437 int v1_1;
438 int v2_1;
439 int v3_1;
440 int toneflags;
441 struct sf_detect_state rd;
442
443 struct dahdi_chan *master; /*!< Our Master channel (could be us) */
444 /*! \brief Next slave (if appropriate) */
445 struct dahdi_chan *nextslave;
446
447 u_char *writechunk; /*!< Actual place to write to */
448 u_char swritechunk[DAHDI_MAX_CHUNKSIZE]; /*!< Buffer to be written */
449 u_char *readchunk; /*!< Actual place to read from */
450 u_char sreadchunk[DAHDI_MAX_CHUNKSIZE]; /*!< Preallocated static area */
451 short *readchunkpreec;
452
453 /* Channel from which to read when DACSed. */
454 struct dahdi_chan *dacs_chan;
455
456 /*! Pointer to tx and rx gain tables */
457 const u_char *rxgain;
458 const u_char *txgain;
459
460 /* Specified by driver, readable by DAHDI */
461 void *pvt; /*!< Private channel data */
462 struct file *file; /*!< File structure */
463
464
465 #ifdef CONFIG_DAHDI_MIRROR
466 struct dahdi_chan *rxmirror; /*!< channel we mirror reads to */
467 struct dahdi_chan *txmirror; /*!< channel we mirror writes to */
468 struct dahdi_chan *srcmirror; /*!< channel we mirror from */
469 #endif /* CONFIG_DAHDI_MIRROR */
470 struct dahdi_span *span; /*!< Span we're a member of */
471 int sig; /*!< Signalling */
472 int sigcap; /*!< Capability for signalling */
473 __u32 chan_alarms; /*!< alarms status */
474
475 wait_queue_head_t waitq;
476
477 /* Used only by DAHDI -- NO DRIVER SERVICEABLE PARTS BELOW */
478 /* Buffer declarations */
479 u_char *readbuf[DAHDI_MAX_NUM_BUFS]; /*!< read buffer */
480 int inreadbuf;
481 int outreadbuf;
482
483 u_char *writebuf[DAHDI_MAX_NUM_BUFS]; /*!< write buffers */
484 int inwritebuf;
485 int outwritebuf;
486
487 int blocksize; /*!< Block size */
488
489 int eventinidx; /*!< out index in event buf (circular) */
490 int eventoutidx; /*!< in index in event buf (circular) */
491 unsigned int eventbuf[DAHDI_MAX_EVENTSIZE]; /*!< event circ. buffer */
492
493 int readn[DAHDI_MAX_NUM_BUFS]; /*!< # of bytes ready in read buf */
494 int readidx[DAHDI_MAX_NUM_BUFS]; /*!< current read pointer */
495 int writen[DAHDI_MAX_NUM_BUFS]; /*!< # of bytes ready in write buf */
496 int writeidx[DAHDI_MAX_NUM_BUFS]; /*!< current write pointer */
497
498 int numbufs; /*!< How many buffers in channel */
499 int txbufpolicy; /*!< Buffer policy */
500 int txdisable; /*!< Disable transmitter */
501
502 /* Tone zone stuff */
503 struct dahdi_zone *curzone; /*!< Zone for selecting tones */
504 struct dahdi_tone *curtone; /*!< Current tone we're playing (if any) */
505 int tonep; /*!< Current position in tone */
506 struct dahdi_tone_state ts; /*!< Tone state */
507
508 /* Pulse dial stuff */
509 int pdialcount; /*!< pulse dial count */
510
511 /*! Ring cadence */
512 int ringcadence[DAHDI_MAX_CADENCE];
513 int firstcadencepos; /*!< Where to restart ring cadence */
514
515 /* Digit string dialing stuff */
516 int digitmode; /*!< What kind of tones are we sending? */
517 char txdialbuf[DAHDI_MAX_DTMF_BUF];
518 int dialing;
519 int afterdialingtimer;
520 int cadencepos; /*!< Where in the cadence we are */
521
522 /* I/O Mask */
523 unsigned int iomask; /*! I/O Mux signal mask */
524
525 /* HDLC state machines */
526 struct fasthdlc_state txhdlc;
527 struct fasthdlc_state rxhdlc;
528 int infcs;
529
530 /* Conferencing stuff */
531 int confna; /*! conference number (alias) */
532 int _confn; /*! Actual conference number */
533 int confmode; /*! conference mode */
534 int confmute; /*! conference mute mode */
535 struct dahdi_chan *conf_chan;
536
537 /* Incoming and outgoing conference chunk queues for
538 communicating between DAHDI master time and
539 other boards */
540 struct confq confin;
541 struct confq confout;
542
543 short getlin[DAHDI_MAX_CHUNKSIZE]; /*!< Last transmitted samples */
544 unsigned char getraw[DAHDI_MAX_CHUNKSIZE]; /*!< Last received raw data */
545 short putlin[DAHDI_MAX_CHUNKSIZE]; /*!< Last received samples */
546 unsigned char putraw[DAHDI_MAX_CHUNKSIZE]; /*!< Last received raw data */
547 short conflast[DAHDI_MAX_CHUNKSIZE]; /*!< Last conference sample -- base part of channel */
548 short conflast1[DAHDI_MAX_CHUNKSIZE]; /*!< Last conference sample -- pseudo part of channel */
549 short conflast2[DAHDI_MAX_CHUNKSIZE]; /*!< Previous last conference sample -- pseudo part of channel */
550
551
552 /*! The echo canceler module that should be used to create an
553 instance when this channel needs one */
554 const struct dahdi_echocan_factory *ec_factory;
555 /*! The echo canceler module that owns the instance currently
556 on this channel, if one is present */
557 const struct dahdi_echocan_factory *ec_current;
558 /*! The state data of the echo canceler instance in use */
559 struct dahdi_echocan_state *ec_state;
560
561 /* RBS timings */
562 int prewinktime; /*!< pre-wink time (ms) */
563 int preflashtime; /*!< pre-flash time (ms) */
564 int winktime; /*!< wink time (ms) */
565 int flashtime; /*!< flash time (ms) */
566 int starttime; /*!< start time (ms) */
567 int rxwinktime; /*!< rx wink time (ms) */
568 int rxflashtime; /*!< rx flash time (ms) */
569 int debouncetime; /*!< FXS GS sig debounce time (ms) */
570 int pulsebreaktime; /*!< pulse line open time (ms) */
571 int pulsemaketime; /*!< pulse line closed time (ms) */
572 int pulseaftertime; /*!< pulse time between digits (ms) */
573
574 /*! RING debounce timer */
575 int ringdebtimer;
576
577 /*! RING trailing detector to make sure a RING is really over */
578 int ringtrailer;
579
580 /* PULSE digit receiver stuff */
581 int pulsecount;
582 int pulsetimer;
583
584 /* RBS timers */
585 int itimerset; /*!< what the itimer was set to last */
586 int itimer;
587 int otimer;
588
589 /* RBS state */
590 int gotgs;
591 int txstate;
592 int rxsig;
593 int txsig;
594 int rxsigstate;
595
596 /* non-RBS rx state */
597 int rxhooksig;
598 int txhooksig;
599 int kewlonhook;
600
601 /*! Idle signalling if CAS signalling */
602 int idlebits;
603
604 int deflaw; /*! 1 = mulaw, 2=alaw, 0=undefined */
605 short *xlaw;
606 #ifdef OPTIMIZE_CHANMUTE
607 int chanmute; /*!< no need for PCM data */
608 #endif
609 #ifdef CONFIG_CALC_XLAW
610 unsigned char (*lineartoxlaw)(short a);
611 #else
612 unsigned char *lin2x;
613 #endif
614 struct device chan_device; /*!< Kernel object for this chan */
615 #define dev_to_chan(dev) container_of(dev, struct dahdi_chan, chan_device)
616 };
617
618 #ifdef CONFIG_DAHDI_NET
619 struct dahdi_hdlc {
620 struct net_device *netdev;
621 struct dahdi_chan *chan;
622 };
623 #endif
624
625 /*! Define the maximum block size */
626 #define DAHDI_MAX_BLOCKSIZE 8192
627
628
629 #define DAHDI_DEFAULT_WINKTIME 150 /*!< 150 ms default wink time */
630 #define DAHDI_DEFAULT_FLASHTIME 750 /*!< 750 ms default flash time */
631
632 #define DAHDI_DEFAULT_PREWINKTIME 50 /*!< 50 ms before wink */
633 #define DAHDI_DEFAULT_PREFLASHTIME 50 /*!< 50 ms before flash */
634 #define DAHDI_DEFAULT_STARTTIME 1500 /*!< 1500 ms of start */
635 #define DAHDI_DEFAULT_RINGTIME 2000 /*!< 2000 ms of ring on (start, FXO) */
636 #if 0
637 #define DAHDI_DEFAULT_RXWINKTIME 250 /*!< 250ms longest rx wink */
638 #endif
639 #define DAHDI_DEFAULT_RXWINKTIME 300 /*!< 300ms longest rx wink (to work with the Atlas) */
640 #define DAHDI_DEFAULT_RXFLASHTIME 1250 /*!< 1250ms longest rx flash */
641 #define DAHDI_DEFAULT_DEBOUNCETIME 600 /*!< 600ms of FXS GS signalling debounce */
642 #define DAHDI_DEFAULT_PULSEMAKETIME 50 /*!< 50 ms of line closed when dial pulsing */
643 #define DAHDI_DEFAULT_PULSEBREAKTIME 50 /*!< 50 ms of line open when dial pulsing */
644 #define DAHDI_DEFAULT_PULSEAFTERTIME 750 /*!< 750ms between dial pulse digits */
645
646 #define DAHDI_MINPULSETIME (15 * 8) /*!< 15 ms minimum */
647
648 #ifdef SHORT_FLASH_TIME
649 #define DAHDI_MAXPULSETIME (80 * 8) /*!< we need 80 ms, not 200ms, as we have a short flash */
650 #else
651 #define DAHDI_MAXPULSETIME (200 * 8) /*!< 200 ms maximum */
652 #endif
653
654 #define DAHDI_PULSETIMEOUT ((DAHDI_MAXPULSETIME / 8) + 50)
655
656 #define DAHDI_RINGTRAILER (50 * 8) /*!< Don't consider a ring "over" until it's been gone at least this
657 much time */
658
659 #define DAHDI_LOOPCODE_TIME 10000 /*!< send loop codes for 10 secs */
660 #define DAHDI_ALARMSETTLE_TIME 5000 /*!< allow alarms to settle for 5 secs */
661 #define DAHDI_AFTERSTART_TIME 500 /*!< 500ms after start */
662
663 #define DAHDI_RINGOFFTIME 4000 /*!< Turn off ringer for 4000 ms */
664 #define DAHDI_KEWLTIME 500 /*!< 500ms for kewl pulse */
665 #define DAHDI_AFTERKEWLTIME 300 /*!< 300ms after kewl pulse */
666
667 #define DAHDI_MAX_PRETRAINING 1000 /*!< 1000ms max pretraining time */
668
669 #ifdef FXSFLASH
670 #define DAHDI_FXSFLASHMINTIME 450 /*!< min 450ms */
671 #define DAHDI_FXSFLASHMAXTIME 550 /*!< max 550ms */
672 #endif
673
674
675 struct dahdi_chardev {
676 const char *name;
677 __u8 minor;
678 };
679
680 int dahdi_register_chardev(struct dahdi_chardev *dev);
681 int dahdi_unregister_chardev(struct dahdi_chardev *dev);
682
683 /*! \brief defines for transmit signalling */
684 enum dahdi_txsig {
685 DAHDI_TXSIG_ONHOOK, /*!< On hook */
686 DAHDI_TXSIG_OFFHOOK, /*!< Off hook */
687 DAHDI_TXSIG_START, /*!< Start / Ring */
688 DAHDI_TXSIG_KEWL, /*!< Drop battery if possible */
689 /*! Leave this as the last entry */
690 DAHDI_TXSIG_TOTAL,
691 };
692
693 enum dahdi_rxsig {
694 DAHDI_RXSIG_ONHOOK,
695 DAHDI_RXSIG_OFFHOOK,
696 DAHDI_RXSIG_START,
697 DAHDI_RXSIG_RING,
698 DAHDI_RXSIG_INITIAL
699 };
700
701 enum {
702 /* Span flags */
703 DAHDI_FLAGBIT_REGISTERED= 0,
704 DAHDI_FLAGBIT_RUNNING = 1,
705 DAHDI_FLAGBIT_RBS = 12, /*!< Span uses RBS signalling */
706
707 /* Channel flags */
708 DAHDI_FLAGBIT_DTMFDECODE= 2, /*!< Channel supports native DTMF decode */
709 DAHDI_FLAGBIT_MFDECODE = 3, /*!< Channel supports native MFr2 decode */
710 DAHDI_FLAGBIT_ECHOCANCEL= 4, /*!< Channel supports native echo cancellation */
711 DAHDI_FLAGBIT_HDLC = 5, /*!< Perform HDLC */
712 #ifdef CONFIG_DAHDI_NET
713 DAHDI_FLAGBIT_NETDEV = 6, /*!< Send to network */
714 #endif
715 DAHDI_FLAGBIT_CLEAR = 8, /*!< Clear channel */
716 DAHDI_FLAGBIT_AUDIO = 9, /*!< Audio mode channel */
717 DAHDI_FLAGBIT_OPEN = 10, /*!< Channel is open */
718 DAHDI_FLAGBIT_FCS = 11, /*!< Calculate FCS */
719 /* Reserve 12 for uniqueness with span flags */
720 DAHDI_FLAGBIT_LINEAR = 13, /*!< Talk to user space in linear */
721 DAHDI_FLAGBIT_PPP = 14, /*!< PPP is available */
722 DAHDI_FLAGBIT_T1PPP = 15,
723 DAHDI_FLAGBIT_SIGFREEZE = 16, /*!< Freeze signalling */
724 DAHDI_FLAGBIT_NOSTDTXRX = 17, /*!< Do NOT do standard transmit and receive on every interrupt */
725 DAHDI_FLAGBIT_LOOPED = 18, /*!< Loopback the receive data from the channel to the transmit */
726 DAHDI_FLAGBIT_MTP2 = 19, /*!< Repeats last message in buffer and also discards repeating messages sent to us */
727 DAHDI_FLAGBIT_HDLC56 = 20, /*!< Sets the given channel (if in HDLC mode) to use 56K HDLC instead of 64K */
728 DAHDI_FLAGBIT_BUFEVENTS = 21, /*!< Report buffer events */
729 DAHDI_FLAGBIT_TXUNDERRUN = 22, /*!< Transmit underrun condition */
730 DAHDI_FLAGBIT_RXOVERRUN = 23, /*!< Receive overrun condition */
731 DAHDI_FLAGBIT_DEVFILE = 25, /*!< Channel has a sysfs dev file */
732 };
733
734 #ifdef CONFIG_DAHDI_NET
735 /**
736 * have_netdev() - Return true if a channel has an associated network device.
737 * @chan: Then channel to check.
738 *
739 */
740 static inline int dahdi_have_netdev(const struct dahdi_chan *chan)
741 {
742 return test_bit(DAHDI_FLAGBIT_NETDEV, &chan->flags);
743 }
744 #else
745 static inline int dahdi_have_netdev(const struct dahdi_chan *chan) { return 0; }
746 #endif
747
748 struct dahdi_count {
749 u32 fe; /*!< Framing error counter */
750 u32 cv; /*!< Coding violations counter */
751 u32 bpv; /*!< Bipolar Violation counter */
752 u32 crc4; /*!< CRC4 error counter */
753 u32 ebit; /*!< current E-bit error count */
754 u32 fas; /*!< current FAS error count */
755 u32 be; /*!< current bit error count */
756 u32 prbs; /*!< current PRBS detected pattern */
757 u32 errsec; /*!< errored seconds */
758 u32 timingslips; /*!< Clock slips */
759 };
760
761 /* map flagbits to flag masks */
762 #define DAHDI_FLAG(x) (1 << (DAHDI_FLAGBIT_ ## x))
763
764 /*! This is a redefinition of the flags from above to allow use of the
765 * legacy drivers that do not use the kernel atomic bit testing and
766 * changing routines.
767 *
768 * See the above descriptions for DAHDI_FLAGBIT_.... for documentation
769 * about function. */
770 /* Span flags */
771 #define DAHDI_FLAG_REGISTERED DAHDI_FLAG(REGISTERED)
772 #define DAHDI_FLAG_RUNNING DAHDI_FLAG(RUNNING)
773 #define DAHDI_FLAG_RBS DAHDI_FLAG(RBS)
774
775 /* Channel flags */
776 #define DAHDI_FLAG_DTMFDECODE DAHDI_FLAG(DTMFDECODE)
777 #define DAHDI_FLAG_MFDECODE DAHDI_FLAG(MFDECODE)
778 #define DAHDI_FLAG_ECHOCANCEL DAHDI_FLAG(ECHOCANCEL)
779
780 #define DAHDI_FLAG_HDLC DAHDI_FLAG(HDLC)
781 /* #define DAHDI_FLAG_NETDEV DAHDI_FLAG(NETDEV) */
782 #define DAHDI_FLAG_CLEAR DAHDI_FLAG(CLEAR)
783 #define DAHDI_FLAG_AUDIO DAHDI_FLAG(AUDIO)
784
785 #define DAHDI_FLAG_OPEN DAHDI_FLAG(OPEN)
786 #define DAHDI_FLAG_FCS DAHDI_FLAG(FCS)
787 /* Reserve 12 for uniqueness with span flags */
788 #define DAHDI_FLAG_LINEAR DAHDI_FLAG(LINEAR)
789 #define DAHDI_FLAG_PPP DAHDI_FLAG(PPP)
790 #define DAHDI_FLAG_T1PPP DAHDI_FLAG(T1PPP)
791 #define DAHDI_FLAG_SIGFREEZE DAHDI_FLAG(SIGFREEZE)
792 #define DAHDI_FLAG_NOSTDTXRX DAHDI_FLAG(NOSTDTXRX)
793 #define DAHDI_FLAG_LOOPED DAHDI_FLAG(LOOPED)
794 #define DAHDI_FLAG_MTP2 DAHDI_FLAG(MTP2)
795 #define DAHDI_FLAG_HDLC56 DAHDI_FLAG(HDLC56)
796 #define DAHDI_FLAG_BUFEVENTS DAHDI_FLAG(BUFEVENTS)
797 #define DAHDI_FLAG_TXUNDERRUN DAHDI_FLAG(TXUNDERRUN)
798 #define DAHDI_FLAG_RXOVERRUN DAHDI_FLAG(RXOVERRUN)
799
800 enum spantypes {
801 SPANTYPE_INVALID = 0,
802 SPANTYPE_ANALOG_FXS,
803 SPANTYPE_ANALOG_FXO,
804 SPANTYPE_ANALOG_MIXED,
805 SPANTYPE_DIGITAL_E1,
806 SPANTYPE_DIGITAL_T1,
807 SPANTYPE_DIGITAL_J1,
808 SPANTYPE_DIGITAL_BRI_NT,
809 SPANTYPE_DIGITAL_BRI_TE,
810 SPANTYPE_DIGITAL_BRI_SOFT,
811 SPANTYPE_DIGITAL_DYNAMIC,
812 };
813 const char *dahdi_spantype2str(enum spantypes st);
814 enum spantypes dahdi_str2spantype(const char *name);
815 const char *dahdi_lineconfig_bit_name(int lineconfig_bit);
816 ssize_t lineconfig_str(int lineconfig, char buf[], size_t size);
817
818 struct file;
819
820 struct dahdi_span_ops {
821 struct module *owner; /*!< Which module is exporting this span. */
822
823 /* ==== Span Callback Operations ==== */
824 /*! Req: Set the requested chunk size. This is the unit in which you must
825 report results for conferencing, etc */
826 int (*setchunksize)(struct dahdi_span *span, int chunksize);
827
828 /*! Opt: Configure the span (if appropriate) */
829 int (*spanconfig)(struct file *file, struct dahdi_span *span,
830 struct dahdi_lineconfig *lc);
831
832 /*! Opt: Start the span */
833 int (*startup)(struct file *file, struct dahdi_span *span);
834
835 /*! Opt: Shutdown the span */
836 int (*shutdown)(struct dahdi_span *span);
837
838 /*! Opt: Enable maintenance modes */
839 int (*maint)(struct dahdi_span *span, int mode);
840
841 #ifdef DAHDI_SYNC_TICK
842 /*! Opt: send sync to spans. Called in hard_irq context with chan_lock
843 * held.*/
844 void (*sync_tick)(struct dahdi_span *span, int is_master);
845 #endif
846 /* ==== Channel Callback Operations ==== */
847 /*! Opt: Set signalling type (if appropriate) */
848 int (*chanconfig)(struct file *file, struct dahdi_chan *chan,
849 int sigtype);
850
851 /*! Opt: Prepare a channel for I/O */
852 int (*open)(struct dahdi_chan *chan);
853
854 /*! Opt: Close channel for I/O */
855 int (*close)(struct dahdi_chan *chan);
856
857 /*! Opt: IOCTL */
858 int (*ioctl)(struct dahdi_chan *chan, unsigned int cmd, unsigned long data);
859
860 /* Okay, now we get to the signalling. You have several options: */
861
862 /* Option 1: If you're a T1 like interface, you can just provide a
863 rbsbits function and we'll assert robbed bits for you. Be sure to
864 set the DAHDI_FLAG_RBS in this case. */
865
866 /*! Opt: If the span uses A/B bits, set them here */
867 int (*rbsbits)(struct dahdi_chan *chan, int bits);
868
869 /*! Option 2: If you don't know about sig bits, but do have their
870 equivalents (i.e. you can disconnect battery, detect off hook,
871 generate ring, etc directly) then you can just specify a
872 sethook function, and we'll call you with appropriate hook states
873 to set. Still set the DAHDI_FLAG_RBS in this case as well */
874 int (*hooksig)(struct dahdi_chan *chan, enum dahdi_txsig hookstate);
875
876 /*! Option 3: If you can't use sig bits, you can write a function
877 which handles the individual hook states */
878 int (*sethook)(struct dahdi_chan *chan, int hookstate);
879
880 /*! Opt: Used to tell an onboard HDLC controller that there is data ready to transmit */
881 void (*hdlc_hard_xmit)(struct dahdi_chan *chan);
882
883 /*! If the watchdog detects no received data, it will call the
884 watchdog routine */
885 int (*watchdog)(struct dahdi_span *span, int cause);
886
887 #ifdef DAHDI_AUDIO_NOTIFY
888 /*! Opt: audio is used, don't optimize out */
889 int (*audio_notify)(struct dahdi_chan *chan, int yes);
890 #endif
891
892 /*! Opt: Enable preechocan stream from inline HW echocanceler. */
893 int (*enable_hw_preechocan)(struct dahdi_chan *chan);
894
895 /*! Opt: Disable preechocan stream from inline HW echocanceler. */
896 void (*disable_hw_preechocan)(struct dahdi_chan *chan);
897
898 /*! Opt: Dacs the contents of chan2 into chan1 if possible */
899 int (*dacs)(struct dahdi_chan *chan1, struct dahdi_chan *chan2);
900
901 /*! Opt: Provide echo cancellation on a channel */
902 int (*echocan_create)(struct dahdi_chan *chan,
903 struct dahdi_echocanparams *ecp,
904 struct dahdi_echocanparam *p,
905 struct dahdi_echocan_state **ec);
906
907 /*! Opt: Provide the name of the echo canceller on a channel */
908 const char *(*echocan_name)(const struct dahdi_chan *chan);
909
910 /*! When using "assigned spans", this function is called back when this
911 * span has been assigned with the system. */
912 void (*assigned)(struct dahdi_span *span);
913
914 /*! Called when the spantype / linemode is changed before the span is
915 * assigned a number. */
916 int (*set_spantype)(struct dahdi_span *span, enum spantypes st);
917 };
918
919 /**
920 * dahdi_device - Represents a device that can contain one or more spans.
921 *
922 * @spans: List of child spans.
923 * @manufacturer: Device manufacturer.
924 * @location: The location of this device. This should not change if
925 * the device is replaced (e.g: in the same PCI slot)
926 * @hardware_id: The hardware_id of this device (NULL for devices without
927 * a hardware_id). This should not change if the device is
928 * relocated to a different location (e.g: different PCI slot)
929 * @devicetype: What type of device this is.
930 * @irqmisses: Count of "interrupt misses" for this device.
931 *
932 */
933 struct dahdi_device {
934 struct list_head spans;
935 const char *manufacturer;
936 const char *location;
937 const char *hardware_id;
938 const char *devicetype;
939 struct device dev;
940 unsigned int irqmisses;
941 struct timespec registration_time;
942 };
943
944 struct dahdi_span {
945 spinlock_t lock;
946 char name[40]; /*!< Span name */
947 char desc[80]; /*!< Span description */
948 enum spantypes spantype; /*!< span type */
949 int deflaw; /*!< Default law (DAHDI_MULAW or DAHDI_ALAW) */
950 int alarms; /*!< Pending alarms on span */
951 unsigned long flags;
952 u8 cannot_provide_timing:1;
953 int lbo; /*!< Span Line-Buildout */
954 int lineconfig; /*!< Span line configuration */
955 int linecompat; /*!< Span line compatibility (0 for
956 analog spans)*/
957 int channels; /*!< Number of channels in span */
958 int txlevel; /*!< Tx level */
959 int rxlevel; /*!< Rx level */
960 int syncsrc; /*!< current sync src (gets copied here) */
961 struct dahdi_count count; /*!< Performance and Error counters */
962
963 int maintstat; /*!< Maintenance state */
964 int mainttimer; /*!< Maintenance timer */
965
966 struct dahdi_chan **chans; /*!< Member channel structures */
967
968 const struct dahdi_span_ops *ops; /*!< span callbacks. */
969
970 /* Used by DAHDI only -- no user servicable parts inside */
971 int spanno; /*!< Span number for DAHDI */
972 int offset; /*!< Offset within a given card */
973 int lastalarms; /*!< Previous alarms */
974
975 #ifdef CONFIG_DAHDI_WATCHDOG
976 int watchcounter;
977 int watchstate;
978 #endif
979
980 #ifdef CONFIG_PROC_FS
981 struct proc_dir_entry *proc_entry;
982 #endif
983 struct list_head spans_node;
984
985 struct dahdi_device *parent;
986 struct list_head device_node;
987 struct device *span_device;
988 };
989
990 struct dahdi_transcoder_channel {
991 void *pvt;
992 struct dahdi_transcoder *parent;
993 wait_queue_head_t ready;
994 __u32 built_fmts;
995 #define DAHDI_TC_FLAG_BUSY 1
996 #define DAHDI_TC_FLAG_CHAN_BUILT 2
997 #define DAHDI_TC_FLAG_NONBLOCK 3
998 #define DAHDI_TC_FLAG_DATA_WAITING 4
999 unsigned long flags;
1000 u32 dstfmt;
1001 u32 srcfmt;
1002 };
1003
1004 int dahdi_is_sync_master(const struct dahdi_span *span);
1005 struct dahdi_span *get_master_span(void);
1006 void set_master_span(int spanno);
1007
1008 static inline int
1009 dahdi_tc_is_built(struct dahdi_transcoder_channel *dtc) {
1010 return test_bit(DAHDI_TC_FLAG_CHAN_BUILT, &dtc->flags);
1011 }
1012 static inline void
1013 dahdi_tc_set_built(struct dahdi_transcoder_channel *dtc) {
1014 set_bit(DAHDI_TC_FLAG_CHAN_BUILT, &dtc->flags);
1015 }
1016 static inline void
1017 dahdi_tc_clear_built(struct dahdi_transcoder_channel *dtc) {
1018 clear_bit(DAHDI_TC_FLAG_CHAN_BUILT, &dtc->flags);
1019 }
1020 static inline int
1021 dahdi_tc_is_nonblock(struct dahdi_transcoder_channel *dtc) {
1022 return test_bit(DAHDI_TC_FLAG_NONBLOCK, &dtc->flags);
1023 }
1024 static inline void
1025 dahdi_tc_set_nonblock(struct dahdi_transcoder_channel *dtc) {
1026 set_bit(DAHDI_TC_FLAG_NONBLOCK, &dtc->flags);
1027 }
1028 static inline void
1029 dahdi_tc_clear_nonblock(struct dahdi_transcoder_channel *dtc) {
1030 clear_bit(DAHDI_TC_FLAG_NONBLOCK, &dtc->flags);
1031 }
1032 static inline int
1033 dahdi_tc_is_data_waiting(struct dahdi_transcoder_channel *dtc) {
1034 return test_bit(DAHDI_TC_FLAG_DATA_WAITING, &dtc->flags);
1035 }
1036 static inline int
1037 dahdi_tc_is_busy(struct dahdi_transcoder_channel *dtc) {
1038 return test_bit(DAHDI_TC_FLAG_BUSY, &dtc->flags);
1039 }
1040 static inline void
1041 dahdi_tc_set_busy(struct dahdi_transcoder_channel *dtc) {
1042 set_bit(DAHDI_TC_FLAG_BUSY, &dtc->flags);
1043 }
1044 static inline void
1045 dahdi_tc_clear_busy(struct dahdi_transcoder_channel *dtc) {
1046 clear_bit(DAHDI_TC_FLAG_BUSY, &dtc->flags);
1047 }
1048 static inline void
1049 dahdi_tc_set_data_waiting(struct dahdi_transcoder_channel *dtc) {
1050 set_bit(DAHDI_TC_FLAG_DATA_WAITING, &dtc->flags);
1051 }
1052 static inline void
1053 dahdi_tc_clear_data_waiting(struct dahdi_transcoder_channel *dtc) {
1054 clear_bit(DAHDI_TC_FLAG_DATA_WAITING, &dtc->flags);
1055 }
1056
1057 struct dahdi_transcoder {
1058 struct list_head active_list_node;
1059 struct list_head registration_list_node;
1060 char name[80];
1061 int numchannels;
1062 unsigned int srcfmts;
1063 unsigned int dstfmts;
1064 struct file_operations fops;
1065 int (*allocate)(struct dahdi_transcoder_channel *channel);
1066 int (*release)(struct dahdi_transcoder_channel *channel);
1067 /* Transcoder channels */
1068 struct dahdi_transcoder_channel channels[0];
1069 };
1070
1071 #define DAHDI_WATCHDOG_NOINTS (1 << 0)
1072
1073 #define DAHDI_WATCHDOG_INIT 1000
1074
1075 #define DAHDI_WATCHSTATE_UNKNOWN 0
1076 #define DAHDI_WATCHSTATE_OK 1
1077 #define DAHDI_WATCHSTATE_RECOVERING 2
1078 #define DAHDI_WATCHSTATE_FAILED 3
1079
1080
1081 struct dahdi_dynamic {
1082 char addr[40];
1083 char dname[20];
1084 int err;
1085 struct kref kref;
1086 long rxjif;
1087 unsigned short txcnt;
1088 unsigned short rxcnt;
1089 struct dahdi_device *ddev;
1090 struct dahdi_span span;
1091 struct dahdi_chan *chans[256];
1092 struct dahdi_dynamic_driver *driver;
1093 void *pvt;
1094 int timing;
1095 int master;
1096 unsigned char *msgbuf;
1097 struct device *dev;
1098
1099 struct list_head list;
1100 };
1101
1102 struct dahdi_dynamic_driver {
1103 /*! Driver name (e.g. Eth) */
1104 const char *name;
1105
1106 /*! Driver description */
1107 const char *desc;
1108
1109 /*! Create a new transmission pipe */
1110 int (*create)(struct dahdi_dynamic *d, const char *address);
1111
1112 /*! Destroy a created transmission pipe */
1113 void (*destroy)(struct dahdi_dynamic *d);
1114
1115 /*! Transmit a given message */
1116 void (*transmit)(struct dahdi_dynamic *d, u8 *msg, size_t msglen);
1117
1118 /*! Flush any pending messages */
1119 int (*flush)(void);
1120
1121 struct list_head list;
1122 struct module *owner;
1123
1124 /*! Numberic id of next device created by this driver. */
1125 unsigned int id;
1126 };
1127
1128 /*! \brief Receive a dynamic span message */
1129 void dahdi_dynamic_receive(struct dahdi_span *span, unsigned char *msg, int msglen);
1130
1131 /*! \brief Register a dynamic driver */
1132 int dahdi_dynamic_register_driver(struct dahdi_dynamic_driver *driver);
1133
1134 /*! \brief Unregister a dynamic driver */
1135 void dahdi_dynamic_unregister_driver(struct dahdi_dynamic_driver *driver);
1136
1137 int _dahdi_receive(struct dahdi_span *span);
1138
1139 /*! Receive on a span. The DAHDI interface will handle all the calculations for
1140 all member channels of the span, pulling the data from the readchunk buffer */
1141 static inline int dahdi_receive(struct dahdi_span *span)
1142 {
1143 unsigned long flags;
1144 int ret;
1145 local_irq_save(flags);
1146 ret = _dahdi_receive(span);
1147 local_irq_restore(flags);
1148 return ret;
1149 }
1150
1151 int _dahdi_transmit(struct dahdi_span *span);
1152
1153 /*! Prepare writechunk buffers on all channels for this span */
1154 static inline int dahdi_transmit(struct dahdi_span *span)
1155 {
1156 unsigned long flags;
1157 int ret;
1158 local_irq_save(flags);
1159 ret = _dahdi_transmit(span);
1160 local_irq_restore(flags);
1161 return ret;
1162 }
1163
1164 static inline int dahdi_is_digital_span(const struct dahdi_span *s)
1165 {
1166 return (s->linecompat > 0);
1167 }
1168
1169 static inline int dahdi_is_t1_span(const struct dahdi_span *s)
1170 {
1171 return (s->linecompat & (DAHDI_CONFIG_D4 | DAHDI_CONFIG_ESF |
1172 DAHDI_CONFIG_B8ZS)) > 0;
1173 }
1174
1175 static inline int dahdi_is_e1_span(const struct dahdi_span *s)
1176 {
1177 return dahdi_is_digital_span(s) && !dahdi_is_t1_span(s);
1178 }
1179
1180 /*! Abort the buffer currently being receive with event "event" */
1181 void dahdi_hdlc_abort(struct dahdi_chan *ss, int event);
1182
1183 /*! Indicate to DAHDI that the end of frame was received and rotate buffers */
1184 void dahdi_hdlc_finish(struct dahdi_chan *ss);
1185
1186 /*! Put a chunk of data into the current receive buffer */
1187 void dahdi_hdlc_putbuf(struct dahdi_chan *ss, unsigned char *rxb, int bytes);
1188
1189 /*! Get a chunk of data from the current transmit buffer. Returns -1 if no data
1190 * is left to send, 0 if there is data remaining in the current message to be sent
1191 * and 1 if the currently transmitted message is now done */
1192 int dahdi_hdlc_getbuf(struct dahdi_chan *ss, unsigned char *bufptr, unsigned int *size);
1193
1194 /*! Register a device. Returns 0 on success, -1 on failure. */
1195 struct dahdi_device *dahdi_create_device(void);
1196 int dahdi_register_device(struct dahdi_device *ddev, struct device *parent);
1197 void dahdi_unregister_device(struct dahdi_device *ddev);
1198 void dahdi_free_device(struct dahdi_device *ddev);
1199 void dahdi_init_span(struct dahdi_span *span);
1200
1201 /*! Allocate / free memory for a transcoder */
1202 struct dahdi_transcoder *dahdi_transcoder_alloc(int numchans);
1203 void dahdi_transcoder_free(struct dahdi_transcoder *ztc);
1204
1205 /*! \brief Register a transcoder */
1206 int dahdi_transcoder_register(struct dahdi_transcoder *tc);
1207
1208 /*! \brief Unregister a transcoder */
1209 int dahdi_transcoder_unregister(struct dahdi_transcoder *tc);
1210
1211 /*! \brief Alert a transcoder */
1212 int dahdi_transcoder_alert(struct dahdi_transcoder_channel *ztc);
1213
1214 /*! \brief Gives a name to an LBO */
1215 const char *dahdi_lboname(int lbo);
1216
1217 /*! \brief Tell DAHDI about changes in received rbs bits */
1218 void dahdi_rbsbits(struct dahdi_chan *chan, int bits);
1219
1220 /*! \brief Tell DAHDI abou changes in received signalling */
1221 void dahdi_hooksig(struct dahdi_chan *chan, enum dahdi_rxsig rxsig);
1222
1223 /*! \brief Queue an event on a channel */
1224 void dahdi_qevent_nolock(struct dahdi_chan *chan, int event);
1225
1226 /*! \brief Queue an event on a channel, locking it first */
1227 void dahdi_qevent_lock(struct dahdi_chan *chan, int event);
1228
1229 /*! \brief Notify a change possible change in alarm status on a channel */
1230 void dahdi_alarm_channel(struct dahdi_chan *chan, int alarms);
1231
1232 /*! \brief Notify a change possible change in alarm status on a span */
1233 void dahdi_alarm_notify(struct dahdi_span *span);
1234
1235 /*! \brief Initialize a tone state */
1236 void dahdi_init_tone_state(struct dahdi_tone_state *ts, struct dahdi_tone *zt);
1237
1238 /*! \brief Get a given MF tone struct, suitable for dahdi_tone_nextsample. */
1239 struct dahdi_tone *dahdi_mf_tone(const struct dahdi_chan *chan, char digit, int digitmode);
1240
1241 /*! \brief Convert signalling bits to human readable string */
1242 const char *sigstr(int sig);
1243
1244 /*! \brief Convert alarm bits to human readable string */
1245 int fill_alarm_string(char *buf, int count, int alarms);
1246
1247 /* Echo cancel a receive and transmit chunk for a given channel. This
1248 should be called by the low-level driver as close to the interface
1249 as possible. ECHO CANCELLATION IS NO LONGER AUTOMATICALLY DONE
1250 AT THE DAHDI LEVEL. dahdi_ec_chunk will not echo cancel if it should
1251 not be doing so. rxchunk is modified in-place */
1252 void __dahdi_ec_chunk(struct dahdi_chan *ss, u8 *rxchunk,
1253 const u8 *preecchunk, const u8 *txchunk);
1254
1255 static inline void _dahdi_ec_chunk(struct dahdi_chan *chan,
1256 u8 *rxchunk, const u8 *txchunk)
1257 {
1258 __dahdi_ec_chunk(chan, rxchunk, rxchunk, txchunk);
1259 }
1260
1261 static inline void dahdi_ec_chunk(struct dahdi_chan *ss, unsigned char *rxchunk,
1262 const unsigned char *txchunk)
1263 {
1264 unsigned long flags;
1265 local_irq_save(flags);
1266 _dahdi_ec_chunk(ss, rxchunk, txchunk);
1267 local_irq_restore(flags);
1268 }
1269
1270 void _dahdi_ec_span(struct dahdi_span *span);
1271 static inline void dahdi_ec_span(struct dahdi_span *span)
1272 {
1273 unsigned long flags;
1274 local_irq_save(flags);
1275 _dahdi_ec_span(span);
1276 local_irq_restore(flags);
1277 }
1278
1279 extern struct file_operations *dahdi_transcode_fops;
1280
1281 int dahdi_get_auto_assign_spans(void);
1282
1283 /* Don't use these directly -- they're not guaranteed to
1284 be there. */
1285 extern short __dahdi_mulaw[256];
1286 extern short __dahdi_alaw[256];
1287 #ifdef CONFIG_CALC_XLAW
1288 u_char __dahdi_lineartoulaw(short a);
1289 u_char __dahdi_lineartoalaw(short a);
1290 #else
1291 extern u_char __dahdi_lin2mu[16384];
1292 extern u_char __dahdi_lin2a[16384];
1293 #endif
1294
1295 struct dahdi_dynamic_ops {
1296 struct module *owner;
1297 int (*ioctl)(unsigned int cmd, unsigned long data);
1298 };
1299
1300 /*! \brief Used by dynamic DAHDI -- don't use directly */
1301 void dahdi_set_dynamic_ops(const struct dahdi_dynamic_ops *ops);
1302
1303 /*! \brief Used by DAHDI HPEC module -- don't use directly */
1304 void dahdi_set_hpec_ioctl(int (*func)(unsigned int cmd, unsigned long data));
1305
1306 /*! \brief Used privately by DAHDI. Avoid touching directly */
1307 struct dahdi_tone {
1308 int fac1;
1309 int init_v2_1;
1310 int init_v3_1;
1311
1312 int fac2;
1313 int init_v2_2;
1314 int init_v3_2;
1315
1316 int tonesamples; /*!< How long to play this tone before
1317 going to the next (in samples) */
1318 struct dahdi_tone *next; /* Next tone in this sequence */
1319
1320 int modulate;
1321 };
1322
1323 static inline short dahdi_tone_nextsample(struct dahdi_tone_state *ts, struct dahdi_tone *zt)
1324 {
1325 /* follow the curves, return the sum */
1326
1327 int p;
1328
1329 ts->v1_1 = ts->v2_1;
1330 ts->v2_1 = ts->v3_1;
1331 ts->v3_1 = (zt->fac1 * ts->v2_1 >> 15) - ts->v1_1;
1332
1333 ts->v1_2 = ts->v2_2;
1334 ts->v2_2 = ts->v3_2;
1335 ts->v3_2 = (zt->fac2 * ts->v2_2 >> 15) - ts->v1_2;
1336
1337 /* Return top 16 bits */
1338 if (!ts->modulate) return ts->v3_1 + ts->v3_2;
1339 /* we are modulating */
1340 p = ts->v3_2 - 32768;
1341 if (p < 0) p = -p;
1342 p = ((p * 9) / 10) + 1;
1343 return (ts->v3_1 * p) >> 15;
1344
1345 }
1346
1347 static inline short dahdi_txtone_nextsample(struct dahdi_chan *ss)
1348 {
1349 /* follow the curves, return the sum */
1350
1351 ss->v1_1 = ss->v2_1;
1352 ss->v2_1 = ss->v3_1;
1353 ss->v3_1 = (ss->txtone * ss->v2_1 >> 15) - ss->v1_1;
1354 return ss->v3_1;
1355 }
1356
1357 /* These are the right functions to use. */
1358
1359 #define DAHDI_MULAW(a) (__dahdi_mulaw[(a)])
1360 #define DAHDI_ALAW(a) (__dahdi_alaw[(a)])
1361 #define DAHDI_XLAW(a,c) (c->xlaw[(a)])
1362
1363 #ifdef CONFIG_CALC_XLAW
1364 #define DAHDI_LIN2MU(a) (__dahdi_lineartoulaw((a)))
1365 #define DAHDI_LIN2A(a) (__dahdi_lineartoalaw((a)))
1366
1367 #define DAHDI_LIN2X(a,c) ((c)->lineartoxlaw((a)))
1368
1369 #else
1370 /* Use tables */
1371 #define DAHDI_LIN2MU(a) (__dahdi_lin2mu[((unsigned short)(a)) >> 2])
1372 #define DAHDI_LIN2A(a) (__dahdi_lin2a[((unsigned short)(a)) >> 2])
1373
1374 /* Manipulate as appropriate for x-law */
1375 #define DAHDI_LIN2X(a,c) ((c)->lin2x[((unsigned short)(a)) >> 2])
1376
1377 #endif /* CONFIG_CALC_XLAW */
1378
1379 /* Data formats for capabilities and frames alike (from Asterisk) */
1380 /*! G.723.1 compression */
1381 #define DAHDI_FORMAT_G723_1 (1 << 0)
1382 /*! GSM compression */
1383 #define DAHDI_FORMAT_GSM (1 << 1)
1384 /*! Raw mu-law data (G.711) */
1385 #define DAHDI_FORMAT_ULAW (1 << 2)
1386 /*! Raw A-law data (G.711) */
1387 #define DAHDI_FORMAT_ALAW (1 << 3)
1388 /*! ADPCM (G.726, 32kbps) */
1389 #define DAHDI_FORMAT_G726 (1 << 4)
1390 /*! ADPCM (IMA) */
1391 #define DAHDI_FORMAT_ADPCM (1 << 5)
1392 /*! Raw 16-bit Signed Linear (8000 Hz) PCM */
1393 #define DAHDI_FORMAT_SLINEAR (1 << 6)
1394 /*! LPC10, 180 samples/frame */
1395 #define DAHDI_FORMAT_LPC10 (1 << 7)
1396 /*! G.729A audio */
1397 #define DAHDI_FORMAT_G729A (1 << 8)
1398 /*! SpeeX Free Compression */
1399 #define DAHDI_FORMAT_SPEEX (1 << 9)
1400 /*! iLBC Free Compression */
1401 #define DAHDI_FORMAT_ILBC (1 << 10)
1402 /*! Maximum audio format */
1403 #define DAHDI_FORMAT_MAX_AUDIO (1 << 15)
1404 /*! Maximum audio mask */
1405 #define DAHDI_FORMAT_AUDIO_MASK ((1 << 16) - 1)
1406
1407 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0)
1408 #define refcount_read atomic_read
1409 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 16, 0)
1410
1411 /* DAHDI only was using the xxx_clear_bit variants. */
1412 #ifndef smp_mb__before_atomic
1413 #define smp_mb__before_atomic smp_mb__before_clear_bit
1414 #endif
1415
1416 #ifndef smp_mb__after_atomic
1417 #define smp_mb__after_atomic smp_mb__after_clear_bit
1418 #endif
1419
1420 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 10, 0)
1421 #ifdef RHEL_RELEASE_VERSION
1422 #if RHEL_RELEASE_CODE < RHEL_RELEASE_VERSION(6, 5)
1423 #ifdef CONFIG_PROC_FS
1424 #include <linux/proc_fs.h>
1425 static inline void *PDE_DATA(const struct inode *inode)
1426 {
1427 return PDE(inode)->data;
1428 }
1429 #endif
1430 #endif
1431 #else
1432 #ifdef CONFIG_PROC_FS
1433 #include <linux/proc_fs.h>
1434 static inline void *PDE_DATA(const struct inode *inode)
1435 {
1436 return PDE(inode)->data;
1437 }
1438 #endif
1439 #endif
1440 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 31)
1441 #define KERN_CONT ""
1442 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
1443
1444 # ifndef RHEL_RELEASE_VERSION
1445 /* I'm not sure which 5.x release this was backported into. */
1446 static inline int try_wait_for_completion(struct completion *x)
1447 {
1448 unsigned long flags;
1449 int ret = 1;
1450
1451 spin_lock_irqsave(&x->wait.lock, flags);
1452 if (!x->done)
1453 ret = 0;
1454 else
1455 x->done--;
1456 spin_unlock_irqrestore(&x->wait.lock, flags);
1457 return ret;
1458 }
1459 #endif
1460
1461 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
1462 #ifdef CONFIG_PROC_FS
1463 #include <linux/proc_fs.h>
1464 static inline struct proc_dir_entry *proc_create_data(const char *name,
1465 mode_t mode,
1466 struct proc_dir_entry *parent,
1467 const struct file_operations *proc_fops,
1468 void *data)
1469 {
1470 struct proc_dir_entry *pde = create_proc_entry(name, mode, parent);
1471 if (!pde)
1472 return NULL;
1473 pde->proc_fops = proc_fops;
1474 pde->data = data;
1475 return pde;
1476 }
1477 #endif /* CONFIG_PROC_FS */
1478 #ifndef clamp
1479 #define clamp(x, low, high) min(max(low, x), high)
1480 #endif
1481 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
1482
1483 /* Some distributions backported fatal_signal_pending so we'll use a macro to
1484 * override the inline function definition. */
1485 #define fatal_signal_pending(p) \
1486 (signal_pending((p)) && sigismember(&(p)->pending.signal, SIGKILL))
1487
1488 #ifdef CONFIG_PCI
1489 #ifndef PCIE_LINK_STATE_L0S
1490 #define PCIE_LINK_STATE_L0S 1
1491 #define PCIE_LINK_STATE_L1 2
1492 #define PCIE_LINK_STATE_CLKPM 4
1493 #endif
1494 #define pci_disable_link_state dahdi_pci_disable_link_state
1495 void dahdi_pci_disable_link_state(struct pci_dev *pdev, int state);
1496 #endif /* CONFIG_PCI */
1497
1498 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22)
1499
1500 #define list_first_entry(ptr, type, member) \
1501 list_entry((ptr)->next, type, member)
1502
1503 #define strncasecmp strnicmp
1504
1505 #ifndef __packed
1506 #define __packed __attribute__((packed))
1507 #endif
1508
1509 #include <linux/ctype.h>
1510 /* A define of 'clamp_val' happened to be added in the patch
1511 * linux-2.6-sata-prep-work-for-rhel5-3.patch kernel-2.6.spec that also
1512 * backported support for strcasecmp to some later RHEL/Centos kernels.
1513 * If you have an older kernel that breaks because strcasecmp is already
1514 * defined, somebody out-smarted us. In that case, replace the line below
1515 * with '#if 0' to get the code building, and file a bug report at
1516 * https://issues.asterisk.org/ .
1517 */
1518 #ifndef clamp_val
1519 static inline int strcasecmp(const char *s1, const char *s2)
1520 {
1521 int c1, c2;
1522
1523 do {
1524 c1 = tolower(*s1++);
1525 c2 = tolower(*s2++);
1526 } while (c1 == c2 && c1 != 0);
1527 return c1 - c2;
1528 }
1529 #endif /* clamp_val */
1530
1531 #endif /* 2.6.22 */
1532 #endif /* 2.6.25 */
1533 #endif /* 2.6.26 */
1534 #endif /* 2.6.27 */
1535 #endif /* 2.6.31 */
1536 #endif /* 3.10.0 */
1537 #endif /* 3.16.0 */
1538 #endif /* 4.11.0 */
1539
1540 #ifndef DEFINE_SPINLOCK
1541 #define DEFINE_SPINLOCK(x) spinlock_t x = SPIN_LOCK_UNLOCKED
1542 #endif
1543
1544 #ifndef DEFINE_SEMAPHORE
1545 #define DEFINE_SEMAPHORE(name) \
1546 struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1)
1547 #endif
1548
1549 #ifndef DEFINE_MUTEX
1550 struct mutex {
1551 struct semaphore sem;
1552 };
1553 #define DEFINE_MUTEX(name) \
1554 struct mutex name = { \
1555 .sem = __SEMAPHORE_INITIALIZER((name).sem, 1), \
1556 }
1557 #define mutex_lock(_x) down(&(_x)->sem)
1558 #define mutex_unlock(_x) up(&(_x)->sem)
1559 #define mutex_init(_x) sema_init(&(_x)->sem, 1)
1560 #endif
1561
1562 #ifndef DEFINE_PCI_DEVICE_TABLE
1563 #define DEFINE_PCI_DEVICE_TABLE(_x) \
1564 const struct pci_device_id _x[] __devinitdata
1565 #endif
1566
1567 #ifndef DMA_BIT_MASK
1568 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
1569 #endif
1570
1571 /* WARN_ONCE first showed up in the kernel in 2.6.27 but it may have been
1572 * backported. */
1573 #ifndef WARN_ONCE
1574 #define WARN_ONCE(condition, format...) WARN_ON_ONCE(condition)
1575 #endif
1576
1577 #define DAHDI_CTL 0
1578 #define DAHDI_TRANSCODE 250
1579 #define DAHDI_TIMER 253
1580 #define DAHDI_CHANNEL 254
1581 #define DAHDI_PSEUDO 255
1582
1583 /* prink-wrapper macros */
1584
1585 #define module_printk(level, fmt, args...) \
1586 printk(level "%s: " fmt, THIS_MODULE->name, ## args)
1587
1588 #define DAHDI_PRINTK(level, category, fmt, ...) \
1589 printk(KERN_ ## level "%s%s-%s: " fmt, #level, category, \
1590 THIS_MODULE->name, ## __VA_ARGS__)
1591 #define span_printk(level, category, span, fmt, ...) \
1592 printk(KERN_ ## level "%s%s-%s: span-%d: " fmt, #level, \
1593 category, THIS_MODULE->name, (span)->spanno, ## __VA_ARGS__)
1594 #define chan_printk(level, category, chan, fmt, ...) \
1595 printk(KERN_ ## level "%s%s-%s: chan-%d: " fmt, #level, \
1596 category, THIS_MODULE->name, (chan)->channo, ## __VA_ARGS__)
1597 #define dahdi_err(fmt, ...) DAHDI_PRINTK(ERR, "", fmt, ## __VA_ARGS__)
1598 #define span_info(span, fmt, ...) span_printk(INFO, "", span, fmt, \
1599 ## __VA_ARGS__)
1600 #define span_notice(span, fmt, ...) span_printk(NOTICE, "", span, fmt, \
1601 ## __VA_ARGS__)
1602 #define span_err(span, fmt, ...) span_printk(ERR, "", span, fmt, \
1603 ## __VA_ARGS__)
1604 #define chan_notice(chan, fmt, ...) chan_printk(NOTICE, "", chan, fmt, \
1605 ## __VA_ARGS__)
1606 #define chan_err(chan, fmt, ...) chan_printk(ERR, "", chan, fmt, \
1607 ## __VA_ARGS__)
1608
1609 #ifndef pr_fmt
1610 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1611 #endif
1612
1613 #ifndef pr_err
1614 #define pr_err(fmt, ...) \
1615 printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
1616 #endif
1617 #ifndef pr_warning
1618 #define pr_warning(fmt, ...) \
1619 printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
1620 #endif
1621 #ifndef pr_warn
1622 #define pr_warn pr_warning
1623 #endif
1624 #ifndef pr_notice
1625 #define pr_notice(fmt, ...) \
1626 printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
1627 #endif
1628 #ifndef pr_info
1629 #define pr_info(fmt, ...) \
1630 printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
1631 #endif
1632
1633 /* If KBUILD_MODNAME is not defined in a compilation unit, then the dev_dbg
1634 * macro will not work properly. */
1635 #ifndef KBUILD_MODNAME
1636 #undef dev_dbg
1637 #ifdef DEBUG
1638 #define dev_dbg dev_info
1639 #else
1640 #define dev_dbg(...) do { } while (0)
1641 #endif
1642 #endif
1643
1644 /* The dbg_* ones use a magical variable 'debug' and the user should be
1645 * aware of that.
1646 */
1647 #ifdef DAHDI_PRINK_MACROS_USE_debug
1648 #ifndef BIT /* added in 2.6.24 */
1649 #define BIT(i) (1UL << (i))
1650 #endif
1651 /* Standard debug bit values. Any module may define others. They must
1652 * be of the form DAHDI_DBG_*
1653 */
1654 #define DAHDI_DBG_GENERAL BIT(0)
1655 #define DAHDI_DBG_ASSIGN BIT(1)
1656 #define DAHDI_DBG_DEVICES BIT(7) /* instantiation/destruction etc. */
1657 #define dahdi_dbg(bits, fmt, ...) \
1658 ((void)((debug & (DAHDI_DBG_ ## bits)) && DAHDI_PRINTK(DEBUG, \
1659 "-" #bits, "%s: " fmt, __func__, ## __VA_ARGS__)))
1660 #define span_dbg(bits, span, fmt, ...) \
1661 ((void)((debug & (DAHDI_DBG_ ## bits)) && \
1662 span_printk(DEBUG, "-" #bits, span, "%s: " \
1663 fmt, __func__, ## __VA_ARGS__)))
1664 #define chan_dbg(bits, chan, fmt, ...) \
1665 ((void)((debug & (DAHDI_DBG_ ## bits)) && \
1666 chan_printk(DEBUG, "-" #bits, chan, \
1667 "%s: " fmt, __func__, ## __VA_ARGS__)))
1668 #define dahdi_dev_dbg(bits, dev, fmt, ...) \
1669 do { if (debug & (DAHDI_DBG_ ## bits)) { \
1670 dev_printk(KERN_DEBUG, dev, \
1671 "DBG-%s(%s): " fmt, #bits, __func__, ## __VA_ARGS__); \
1672 } } while (0)
1673
1674 #endif /* DAHDI_PRINK_MACROS_USE_debug */
1675
1676 #endif /* _DAHDI_KERNEL_H */