2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
5 * Copyright (C) 2007, Jim Dixon
7 * Jim Dixon, WB6NIL <jim@lambdatel.com>
8 * Steve Henke, W9SH <w9sh@arrl.net>
9 * Based upon work by Mark Spencer <markster@digium.com> and Luigi Rizzo
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2. See the LICENSE file
19 * at the top of the source tree.
24 * \brief Channel driver for CM108 USB Cards with Radio Interface
26 * \author Jim Dixon <jim@lambdatel.com>
27 * \author Steve Henke <w9sh@arrl.net>
30 * \arg \ref Config_usbradio
32 * \ingroup channel_drivers
36 <depend>asound</depend>
38 <defaultenabled>no</defaultenabled>
43 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
47 #include <sys/ioctl.h>
51 #include <alsa/asoundlib.h>
53 #define CHAN_USBRADIO 1
55 #define DEBUG_USBRADIO 0
56 #define DEBUG_CAPTURES 1
58 #define DEBUG_CAP_RX_OUT 0
59 #define DEBUG_CAP_TX_OUT 0
61 #define DEBUG_FILETEST 0
63 #define RX_CAP_RAW_FILE "/tmp/rx_cap_in.pcm"
64 #define RX_CAP_TRACE_FILE "/tmp/rx_trace.pcm"
65 #define RX_CAP_OUT_FILE "/tmp/rx_cap_out.pcm"
67 #define TX_CAP_RAW_FILE "/tmp/tx_cap_in.pcm"
68 #define TX_CAP_TRACE_FILE "/tmp/tx_trace.pcm"
69 #define TX_CAP_OUT_FILE "/tmp/tx_cap_out.pcm"
71 #define MIXER_PARAM_MIC_PLAYBACK_SW "Mic Playback Switch"
72 #define MIXER_PARAM_MIC_PLAYBACK_VOL "Mic Playback Volume"
73 #define MIXER_PARAM_MIC_CAPTURE_SW "Mic Capture Switch"
74 #define MIXER_PARAM_MIC_CAPTURE_VOL "Mic Capture Volume"
75 #define MIXER_PARAM_MIC_BOOST "Auto Gain Control"
76 #define MIXER_PARAM_SPKR_PLAYBACK_SW "Speaker Playback Switch"
77 #define MIXER_PARAM_SPKR_PLAYBACK_VOL "Speaker Playback Volume"
79 #include "./xpmr/xpmr.h"
82 #define traceusb1(a, ...) ast_debug(4, a __VA_ARGS__)
84 #define traceusb1(a, ...)
88 #define traceusb2(a, ...) ast_debug(4, a __VA_ARGS__)
90 #define traceusb2(a, ...)
94 #include <linux/soundcard.h>
95 #elif defined(__FreeBSD__)
96 #include <sys/soundcard.h>
98 #include <soundcard.h>
101 #include "asterisk/lock.h"
102 #include "asterisk/frame.h"
103 #include "asterisk/callerid.h"
104 #include "asterisk/channel.h"
105 #include "asterisk/module.h"
106 #include "asterisk/pbx.h"
107 #include "asterisk/config.h"
108 #include "asterisk/cli.h"
109 #include "asterisk/utils.h"
110 #include "asterisk/causes.h"
111 #include "asterisk/endian.h"
112 #include "asterisk/stringfields.h"
113 #include "asterisk/abstract_jb.h"
114 #include "asterisk/musiconhold.h"
115 #include "asterisk/dsp.h"
117 /* ringtones we use */
119 #include "ringtone.h"
123 #define C108_VENDOR_ID 0x0d8c
124 #define C108_PRODUCT_ID 0x000c
125 #define C108_HID_INTERFACE 3
127 #define HID_REPORT_GET 0x01
128 #define HID_REPORT_SET 0x09
130 #define HID_RT_INPUT 0x01
131 #define HID_RT_OUTPUT 0x02
133 /*! Global jitterbuffer configuration - by default, jb is disabled */
134 static struct ast_jb_conf default_jbconf =
138 .resync_threshold = -1,
141 static struct ast_jb_conf global_jbconf;
144 * usbradio.conf parameters are
148 ; General config options, with default values shown.
149 ; You should use one section per device, with [general] being used
153 ; debug = 0x0 ; misc debug flags, default is 0
155 ; Set the device to use for I/O
157 ; Set hardware type here
158 ; hdwtype=0 ; 0=limey, 1=sph
160 ; rxboostset=0 ; no rx gain boost
161 ; rxctcssrelax=1 ; reduce talkoff from radios w/o CTCSS Tx HPF
162 ; rxctcssfreq=100.0 ; rx ctcss freq in floating point. must be in table
163 ; txctcssfreq=100.0 ; tx ctcss freq, any frequency permitted
165 ; carrierfrom=dsp ;no,usb,usbinvert,dsp,vox
166 ; ctcssfrom=dsp ;no,usb,dsp
168 ; rxdemod=flat ; input type from radio: no,speaker,flat
169 ; txprelim=yes ; output is pre-emphasised and limited
170 ; txtoctype=no ; no,phase,notone
172 ; txmixa=composite ;no,voice,tone,composite,auxvoice
173 ; txmixb=no ;no,voice,tone,composite,auxvoice
177 ;------------------------------ JITTER BUFFER CONFIGURATION --------------------------
178 ; jbenable = yes ; Enables the use of a jitterbuffer on the receiving side of an
179 ; USBRADIO channel. Defaults to "no". An enabled jitterbuffer will
180 ; be used only if the sending side can create and the receiving
181 ; side can not accept jitter. The USBRADIO channel can't accept jitter,
182 ; thus an enabled jitterbuffer on the receive USBRADIO side will always
183 ; be used if the sending side can create jitter.
185 ; jbmaxsize = 200 ; Max length of the jitterbuffer in milliseconds.
187 ; jbresyncthreshold = 1000 ; Jump in the frame timestamps over which the jitterbuffer is
188 ; resynchronized. Useful to improve the quality of the voice, with
189 ; big jumps in/broken timestamps, usualy sent from exotic devices
190 ; and programs. Defaults to 1000.
192 ; jbimpl = fixed ; Jitterbuffer implementation, used on the receiving side of an USBRADIO
193 ; channel. Two implementations are currenlty available - "fixed"
194 ; (with size always equals to jbmax-size) and "adaptive" (with
195 ; variable size, actually the new jb of IAX2). Defaults to fixed.
197 ; jblog = no ; Enables jitterbuffer frame logging. Defaults to "no".
198 ;-----------------------------------------------------------------------------------
206 * The following parameters are used in the driver:
208 * FRAME_SIZE the size of an audio frame, in samples.
209 * 160 is used almost universally, so you should not change it.
211 * FRAGS the argument for the SETFRAGMENT ioctl.
212 * Overridden by the 'frags' parameter in usbradio.conf
214 * Bits 0-7 are the base-2 log of the device's block size,
215 * bits 16-31 are the number of blocks in the driver's queue.
216 * There are a lot of differences in the way this parameter
217 * is supported by different drivers, so you may need to
218 * experiment a bit with the value.
219 * A good default for linux is 30 blocks of 64 bytes, which
220 * results in 6 frames of 320 bytes (160 samples).
221 * FreeBSD works decently with blocks of 256 or 512 bytes,
222 * leaving the number unspecified.
223 * Note that this only refers to the device buffer size,
224 * this module will then try to keep the lenght of audio
225 * buffered within small constraints.
227 * QUEUE_SIZE The max number of blocks actually allowed in the device
228 * driver's buffer, irrespective of the available number.
229 * Overridden by the 'queuesize' parameter in usbradio.conf
231 * Should be >=2, and at most as large as the hw queue above
232 * (otherwise it will never be full).
235 #define FRAME_SIZE 160
236 #define QUEUE_SIZE 20
238 #if defined(__FreeBSD__)
241 #define FRAGS ( ( (6 * 5) << 16 ) | 0xc )
245 * XXX text message sizes are probably 256 chars, but i am
246 * not sure if there is a suitable definition anywhere.
248 #define TEXT_SIZE 256
251 #define TRYOPEN 1 /* try to open on startup */
253 #define O_CLOSE 0x444 /* special 'close' mode for device */
254 /* Which device to use */
255 #if defined( __OpenBSD__ ) || defined( __NetBSD__ )
256 #define DEV_DSP "/dev/audio"
258 #define DEV_DSP "/dev/dsp"
262 #define MIN(a,b) ((a) < (b) ? (a) : (b))
265 #define MAX(a,b) ((a) > (b) ? (a) : (b))
268 static char *config = "usbradio.conf"; /* default config file */
269 static char *config1 = "usbradio_tune.conf"; /* tune config file */
271 static FILE *frxcapraw = NULL, *frxcaptrace = NULL, *frxoutraw = NULL;
272 static FILE *ftxcapraw = NULL, *ftxcaptrace = NULL, *ftxoutraw = NULL;
274 static int usbradio_debug;
275 #if 0 /* maw asdf sph */
276 static int usbradio_debug_level = 0;
279 enum {RX_AUDIO_NONE,RX_AUDIO_SPEAKER,RX_AUDIO_FLAT};
280 enum {CD_IGNORE,CD_XPMR_NOISE,CD_XPMR_VOX,CD_HID,CD_HID_INVERT};
281 enum {SD_IGNORE,SD_HID,SD_HID_INVERT,SD_XPMR}; /* no,external,externalinvert,software */
282 enum {RX_KEY_CARRIER,RX_KEY_CARRIER_CODE};
283 enum {TX_OUT_OFF,TX_OUT_VOICE,TX_OUT_LSD,TX_OUT_COMPOSITE,TX_OUT_AUX};
284 enum {TOC_NONE,TOC_PHASE,TOC_NOTONE};
286 /* DECLARE STRUCTURES */
289 * Each sound is made of 'datalen' samples of sound, repeated as needed to
290 * generate 'samplen' samples of data, then followed by 'silencelen' samples
291 * of silence. The loop is repeated if 'repeat' is set.
303 static struct sound sounds[] = {
304 { AST_CONTROL_RINGING, "RINGING", ringtone, sizeof(ringtone)/2, 16000, 32000, 1 },
305 { AST_CONTROL_BUSY, "BUSY", busy, sizeof(busy)/2, 4000, 4000, 1 },
306 { AST_CONTROL_CONGESTION, "CONGESTION", busy, sizeof(busy)/2, 2000, 2000, 1 },
307 { AST_CONTROL_RING, "RING10", ring10, sizeof(ring10)/2, 16000, 32000, 1 },
308 { AST_CONTROL_ANSWER, "ANSWER", answer, sizeof(answer)/2, 2200, 0, 0 },
309 { -1, NULL, 0, 0, 0, 0 }, /* end marker */
314 * descriptor for one of our channels.
315 * There is one used for 'default' values (from the [general] entry in
316 * the configuration file), and then one instance for each device
317 * (the default is cloned from [general], others are only created
318 * if the relevant section exists).
320 struct chan_usbradio_pvt {
321 struct chan_usbradio_pvt *next;
325 * cursound indicates which in struct sound we play. -1 means nothing,
326 * any other value is a valid sound, in which case sampsent indicates
327 * the next sample to send in [0..samplen + silencelen]
328 * nosound is set to disable the audio data from the channel
329 * (so we can play the tones etc.).
331 int sndcmd[2]; /* Sound command pipe */
332 int cursound; /* index of sound to send */
333 int sampsent; /* # of sound samples sent */
334 int nosound; /* set to block audio from the PBX */
336 int total_blocks; /* total blocks in the output device */
338 enum { M_UNSET, M_FULL, M_READ, M_WRITE } duplex;
343 unsigned int queuesize; /* max fragments in queue */
344 unsigned int frags; /* parameter for SETFRAGMENT */
346 int warned; /* various flags used for warnings */
347 #define WARN_used_blocks 1
350 int w_errors; /* overfull in the write path */
351 struct timeval lastopen;
356 /* boost support. BOOST_SCALE * 10 ^(BOOST_MAX/20) must
357 * be representable in 16 bits to avoid overflows.
359 #define BOOST_SCALE (1<<9)
360 #define BOOST_MAX 40 /* slightly less than 7 bits */
361 int boost; /* input boost, scaled by BOOST_SCALE */
370 struct ast_channel *owner;
371 char ext[AST_MAX_EXTENSION];
372 char ctx[AST_MAX_CONTEXT];
373 char language[MAX_LANGUAGE];
374 char cid_name[256]; /* XXX */
375 char cid_num[256]; /* XXX */
376 char mohinterpret[MAX_MUSICCLASS];
378 /* buffers used in usbradio_write, 2 per int by 2 channels by 6 times oversampling (48KS/s) */
379 char usbradio_write_buf[FRAME_SIZE * 2 * 2 * 6];
380 char usbradio_write_buf_1[FRAME_SIZE * 2 * 2 * 6];
382 int usbradio_write_dst;
383 /* buffers used in usbradio_read - AST_FRIENDLY_OFFSET space for headers
384 * plus enough room for a full frame
386 char usbradio_read_buf[FRAME_SIZE * (2 * 12) + AST_FRIENDLY_OFFSET];
387 char usbradio_read_buf_8k[FRAME_SIZE * 2 + AST_FRIENDLY_OFFSET];
388 int readpos; /* read position above */
389 struct ast_frame read_f; /* returned by usbradio_read */
397 char rxcarrierdetect; /*!< status from pmr channel */
398 char rxctcssdecode; /*!< status from pmr channel */
401 char rxkeyed; /*!< indicates rx signal present */
404 char txkeyed; /*! tx key request from upper layers */
420 int rxsquelchadj; /*!< this copy needs to be here for initialization */
445 int hid_gpio_ctl_loc;
449 int hid_io_ctcss_loc;
461 /* maw add additional defaults !!! */
462 static struct chan_usbradio_pvt usbradio_default = {
465 .duplex = M_UNSET, /* XXX check this */
468 .queuesize = QUEUE_SIZE,
472 .readpos = AST_FRIENDLY_OFFSET, /* start here on reads */
473 .lastopen = { 0, 0 },
474 .boost = BOOST_SCALE,
477 /* DECLARE FUNCTION PROTOTYPES */
479 static void store_txtoctype(struct chan_usbradio_pvt *o, const char *s);
480 static int hidhdwconfig(struct chan_usbradio_pvt *o);
481 static int set_txctcss_level(struct chan_usbradio_pvt *o);
482 static void pmrdump(struct chan_usbradio_pvt *o);
483 static void mult_set(struct chan_usbradio_pvt *o);
484 static int mult_calc(int value);
485 static void mixer_write(struct chan_usbradio_pvt *o);
486 static void tune_rxinput(struct chan_usbradio_pvt *o);
487 static void tune_rxvoice(struct chan_usbradio_pvt *o);
488 static void tune_rxctcss(struct chan_usbradio_pvt *o);
489 static void tune_txoutput(struct chan_usbradio_pvt *o, int value);
490 static void tune_write(struct chan_usbradio_pvt *o);
492 static char *usbradio_active; /* the active device */
494 static int setformat(struct chan_usbradio_pvt *o, int mode);
496 static struct ast_channel *usbradio_request(const char *type, int format, void *data
498 static int usbradio_digit_begin(struct ast_channel *c, char digit);
499 static int usbradio_digit_end(struct ast_channel *c, char digit, unsigned int duration);
500 static int usbradio_text(struct ast_channel *c, const char *text);
501 static int usbradio_hangup(struct ast_channel *c);
502 static int usbradio_answer(struct ast_channel *c);
503 static struct ast_frame *usbradio_read(struct ast_channel *chan);
504 static int usbradio_call(struct ast_channel *c, char *dest, int timeout);
505 static int usbradio_write(struct ast_channel *chan, struct ast_frame *f);
506 static int usbradio_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen);
507 static int usbradio_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
509 #if DEBUG_FILETEST == 1
510 static int RxTestIt(struct chan_usbradio_pvt *o);
513 static char tdesc[] = "USB (CM108) Radio Channel Driver";
515 static const struct ast_channel_tech usbradio_tech = {
517 .description = tdesc,
518 .capabilities = AST_FORMAT_SLINEAR,
519 .requester = usbradio_request,
520 .send_digit_begin = usbradio_digit_begin,
521 .send_digit_end = usbradio_digit_end,
522 .send_text = usbradio_text,
523 .hangup = usbradio_hangup,
524 .answer = usbradio_answer,
525 .read = usbradio_read,
526 .call = usbradio_call,
527 .write = usbradio_write,
528 .indicate = usbradio_indicate,
529 .fixup = usbradio_fixup,
532 /* Call with: devnum: alsa major device number, param: ascii Formal
533 Parameter Name, val1, first or only value, val2 second value, or 0
534 if only 1 value. Values: 0-99 (percent) or 0-1 for baboon.
536 Note: must add -lasound to end of linkage */
538 static int amixer_max(int devnum,char *param)
543 snd_ctl_elem_id_t *id;
544 snd_hctl_elem_t *elem;
545 snd_ctl_elem_info_t *info;
547 snprintf(str, sizeof(str), "hw:%d", devnum);
548 if (snd_hctl_open(&hctl, str, 0))
551 id = alloca(snd_ctl_elem_id_sizeof());
552 memset(id, 0, snd_ctl_elem_id_sizeof());
553 snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
554 snd_ctl_elem_id_set_name(id, param);
555 elem = snd_hctl_find_elem(hctl, id);
557 snd_hctl_close(hctl);
560 info = alloca(snd_ctl_elem_info_sizeof());
561 memset(info, 0, snd_ctl_elem_info_sizeof());
562 snd_hctl_elem_info(elem,info);
563 type = snd_ctl_elem_info_get_type(info);
566 case SND_CTL_ELEM_TYPE_INTEGER:
567 rv = snd_ctl_elem_info_get_max(info);
569 case SND_CTL_ELEM_TYPE_BOOLEAN:
573 snd_hctl_close(hctl);
577 /*! \brief Call with: devnum: alsa major device number, param: ascii Formal
578 Parameter Name, val1, first or only value, val2 second value, or 0
579 if only 1 value. Values: 0-99 (percent) or 0-1 for baboon.
581 Note: must add -lasound to end of linkage */
583 static int setamixer(int devnum, char *param, int v1, int v2)
588 snd_ctl_elem_id_t *id;
589 snd_ctl_elem_value_t *control;
590 snd_hctl_elem_t *elem;
591 snd_ctl_elem_info_t *info;
593 snprintf(str, sizeof(str), "hw:%d", devnum);
594 if (snd_hctl_open(&hctl, str, 0))
597 id = alloca(snd_ctl_elem_id_sizeof());
598 memset(id, 0, snd_ctl_elem_id_sizeof());
599 snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
600 snd_ctl_elem_id_set_name(id, param);
601 elem = snd_hctl_find_elem(hctl, id);
603 snd_hctl_close(hctl);
606 info = alloca(snd_ctl_elem_info_sizeof());
607 memset(info, 0, snd_ctl_elem_info_sizeof());
608 snd_hctl_elem_info(elem,info);
609 type = snd_ctl_elem_info_get_type(info);
610 control = alloca(snd_ctl_elem_value_sizeof());
611 memset(control, 0, snd_ctl_elem_value_sizeof());
612 snd_ctl_elem_value_set_id(control, id);
614 case SND_CTL_ELEM_TYPE_INTEGER:
615 snd_ctl_elem_value_set_integer(control, 0, v1);
616 if (v2 > 0) snd_ctl_elem_value_set_integer(control, 1, v2);
618 case SND_CTL_ELEM_TYPE_BOOLEAN:
619 snd_ctl_elem_value_set_integer(control, 0, (v1 != 0));
622 if (snd_hctl_elem_write(elem, control)) {
623 snd_hctl_close(hctl);
626 snd_hctl_close(hctl);
630 static void hid_set_outputs(struct usb_dev_handle *handle,
631 unsigned char *outputs)
633 usb_control_msg(handle,
634 USB_ENDPOINT_OUT + USB_TYPE_CLASS + USB_RECIP_INTERFACE,
636 0 + (HID_RT_OUTPUT << 8),
638 (char *)outputs, 4, 5000);
641 static void hid_get_inputs(struct usb_dev_handle *handle,
642 unsigned char *inputs)
644 usb_control_msg(handle,
645 USB_ENDPOINT_IN + USB_TYPE_CLASS + USB_RECIP_INTERFACE,
647 0 + (HID_RT_INPUT << 8),
649 (char *)inputs, 4, 5000);
652 static struct usb_device *hid_device_init(void)
654 struct usb_bus *usb_bus;
655 struct usb_device *dev;
660 for (usb_bus = usb_busses; usb_bus; usb_bus = usb_bus->next) {
661 for (dev = usb_bus->devices; dev; dev = dev->next) {
662 if ((dev->descriptor.idVendor == C108_VENDOR_ID) && (dev->descriptor.idProduct == C108_PRODUCT_ID))
669 static int hidhdwconfig(struct chan_usbradio_pvt *o)
671 if (o->hdwtype == 1) { /*sphusb */
672 o->hid_gpio_ctl = 0x08; /* set GPIO4 to output mode */
673 o->hid_gpio_ctl_loc = 2; /* For CTL of GPIO */
674 o->hid_io_cor = 4; /* GPIO3 is COR */
675 o->hid_io_cor_loc = 1; /* GPIO3 is COR */
676 o->hid_io_ctcss = 2; /* GPIO 2 is External CTCSS */
677 o->hid_io_ctcss_loc = 1; /* is GPIO 2 */
678 o->hid_io_ptt = 8; /* GPIO 4 is PTT */
679 o->hid_gpio_loc = 1; /* For ALL GPIO */
680 } else if (o->hdwtype == 0) { /* dudeusb */
681 o->hid_gpio_ctl = 0x0c;/* set GPIO 3 & 4 to output mode */
682 o->hid_gpio_ctl_loc = 2; /* For CTL of GPIO */
683 o->hid_io_cor = 2; /* VOLD DN is COR */
684 o->hid_io_cor_loc = 0; /* VOL DN COR */
685 o->hid_io_ctcss = 2; /* GPIO 2 is External CTCSS */
686 o->hid_io_ctcss_loc = 1; /* is GPIO 2 */
687 o->hid_io_ptt = 4; /* GPIO 3 is PTT */
688 o->hid_gpio_loc = 1; /* For ALL GPIO */
689 } else if (o->hdwtype == 3) { /* custom version */
690 o->hid_gpio_ctl = 0x0c; /* set GPIO 3 & 4 to output mode */
691 o->hid_gpio_ctl_loc = 2; /* For CTL of GPIO */
692 o->hid_io_cor = 2; /* VOLD DN is COR */
693 o->hid_io_cor_loc = 0; /* VOL DN COR */
694 o->hid_io_ctcss = 2; /* GPIO 2 is External CTCSS */
695 o->hid_io_ctcss_loc = 1; /* is GPIO 2 */
696 o->hid_io_ptt = 4; /* GPIO 3 is PTT */
697 o->hid_gpio_loc = 1; /* For ALL GPIO */
704 static void *hidthread(void *arg)
706 unsigned char buf[4], keyed;
708 struct usb_device *usb_dev;
709 struct usb_dev_handle *usb_handle;
710 struct chan_usbradio_pvt *o = arg;
712 usb_dev = hid_device_init();
713 if (usb_dev == NULL) {
714 ast_log(LOG_ERROR, "USB HID device not found\n");
717 usb_handle = usb_open(usb_dev);
718 if (usb_handle == NULL) {
719 ast_log(LOG_ERROR, "Not able to open USB device\n");
722 if (usb_claim_interface(usb_handle, C108_HID_INTERFACE) < 0) {
723 if (usb_detach_kernel_driver_np(usb_handle, C108_HID_INTERFACE) < 0) {
724 ast_log(LOG_ERROR, "Not able to detach the USB device\n");
727 if (usb_claim_interface(usb_handle, C108_HID_INTERFACE) < 0) {
728 ast_log(LOG_ERROR, "Not able to claim the USB device\n");
732 memset(buf, 0, sizeof(buf));
733 buf[2] = o->hid_gpio_ctl;
735 hid_set_outputs(usb_handle, buf);
736 traceusb1("hidthread: Starting normally!!\n");
738 while (!o->stophid) {
739 buf[o->hid_gpio_ctl_loc] = o->hid_gpio_ctl;
740 hid_get_inputs(usb_handle, buf);
741 keyed = !(buf[o->hid_io_cor_loc] & o->hid_io_cor);
742 if (keyed != o->rxhidsq) {
744 ast_log(LOG_NOTICE, "chan_usbradio() hidthread: update rxhidsq = %d\n", keyed);
748 /* if change in tx stuff */
750 if (o->txkeyed || o->txchankey || o->txtestkey || o->pmrChan->txPttOut)
753 if (o->lasttx != txtmp) {
756 ast_log(LOG_NOTICE, "hidthread: tx set to %d\n", txtmp);
757 buf[o->hid_gpio_loc] = 0;
759 buf[o->hid_gpio_loc] = o->hid_io_ptt;
760 buf[o->hid_gpio_ctl_loc] = o->hid_gpio_ctl;
761 hid_set_outputs(usb_handle, buf);
764 time(&o->lasthidtime);
767 buf[o->hid_gpio_loc] = 0;
769 buf[o->hid_gpio_loc] = o->hid_io_ptt;
770 buf[o->hid_gpio_ctl_loc] = o->hid_gpio_ctl;
771 hid_set_outputs(usb_handle, buf);
776 * returns a pointer to the descriptor with the given name
778 static struct chan_usbradio_pvt *find_desc(char *dev)
780 struct chan_usbradio_pvt *o = NULL;
783 ast_log(LOG_WARNING, "null dev\n");
785 for (o = usbradio_default.next; o && o->name && dev && strcmp(o->name, dev) != 0; o = o->next);
788 ast_log(LOG_WARNING, "could not find <%s>\n", dev ? dev : "--no-device--");
794 * split a string in extension-context, returns pointers to malloc'ed
796 * If we do not have 'overridecontext' then the last @ is considered as
797 * a context separator, and the context is overridden.
798 * This is usually not very necessary as you can play with the dialplan,
799 * and it is nice not to need it because you have '@' in SIP addresses.
800 * Return value is the buffer address.
803 static char *ast_ext_ctx(const char *src, char **ext, char **ctx)
805 struct chan_usbradio_pvt *o = find_desc(usbradio_active);
807 if (ext == NULL || ctx == NULL)
808 return NULL; /* error */
812 if (src && *src != '\0')
813 *ext = ast_strdup(src);
818 if (!o->overridecontext) {
819 /* parse from the right */
820 *ctx = strrchr(*ext, '@');
830 * Returns the number of blocks used in the audio output channel
832 static int used_blocks(struct chan_usbradio_pvt *o)
834 struct audio_buf_info info;
836 if (ioctl(o->sounddev, SNDCTL_DSP_GETOSPACE, &info)) {
837 if (!(o->warned & WARN_used_blocks)) {
838 ast_log(LOG_WARNING, "Error reading output space\n");
839 o->warned |= WARN_used_blocks;
844 if (o->total_blocks == 0) {
845 ast_debug(4, "fragtotal %d size %d avail %d\n", info.fragstotal, info.fragsize, info.fragments);
846 o->total_blocks = info.fragments;
849 return o->total_blocks - info.fragments;
852 /*! \brief Write an exactly FRAME_SIZE sized frame */
853 static int soundcard_writeframe(struct chan_usbradio_pvt *o, short *data)
858 setformat(o, O_RDWR);
860 return 0; /* not fatal */
862 * Nothing complex to manage the audio device queue.
863 * If the buffer is full just drop the extra, otherwise write.
864 * XXX in some cases it might be useful to write anyways after
865 * a number of failures, to restart the output chain.
867 res = used_blocks(o);
868 if (res > o->queuesize) { /* no room to write a block */
869 if (o->w_errors++ == 0 && (usbradio_debug & 0x4))
870 ast_log(LOG_WARNING, "write: used %d blocks (%d)\n", res, o->w_errors);
875 return write(o->sounddev, ((void *) data), FRAME_SIZE * 2 * 12);
879 * Handler for 'sound writable' events from the sound thread.
880 * Builds a frame from the high level description of the sounds,
881 * and passes it to the audio device.
882 * The actual sound is made of 1 or more sequences of sound samples
883 * (s->datalen, repeated to make s->samplen samples) followed by
884 * s->silencelen samples of silence. The position in the sequence is stored
885 * in o->sampsent, which goes between 0 .. s->samplen+s->silencelen.
886 * In case we fail to write a frame, don't update o->sampsent.
888 static void send_sound(struct chan_usbradio_pvt *o)
890 short myframe[FRAME_SIZE];
892 int l_sampsent = o->sampsent;
895 if (o->cursound < 0) /* no sound to send */
898 s = &sounds[o->cursound];
900 for (ofs = 0; ofs < FRAME_SIZE; ofs += l) {
901 l = s->samplen - l_sampsent; /* # of available samples */
903 start = l_sampsent % s->datalen; /* source offset */
904 if (l > FRAME_SIZE - ofs) /* don't overflow the frame */
905 l = FRAME_SIZE - ofs;
906 if (l > s->datalen - start) /* don't overflow the source */
907 l = s->datalen - start;
908 memcpy(myframe + ofs, s->data + start, l * 2);
909 ast_debug(4, "send_sound sound %d/%d of %d into %d\n", l_sampsent, l, s->samplen, ofs);
911 } else { /* end of samples, maybe some silence */
912 static const short silence[FRAME_SIZE] = { 0, };
916 if (l > FRAME_SIZE - ofs)
917 l = FRAME_SIZE - ofs;
918 memcpy(myframe + ofs, silence, l * 2);
920 } else { /* silence is over, restart sound if loop */
921 if (s->repeat == 0) { /* last block */
923 o->nosound = 0; /* allow audio data */
924 if (ofs < FRAME_SIZE) /* pad with silence */
925 memcpy(myframe + ofs, silence, (FRAME_SIZE - ofs) * 2);
931 l = soundcard_writeframe(o, myframe);
933 o->sampsent = l_sampsent; /* update status */
936 static void *sound_thread(void *arg)
939 struct chan_usbradio_pvt *o = arg;
942 * Just in case, kick the driver by trying to read from it.
943 * Ignore errors - this read is almost guaranteed to fail.
945 read(o->sounddev, ign, sizeof(ign));
952 FD_SET(o->sndcmd[0], &rfds);
953 maxfd = o->sndcmd[0]; /* pipe from the main process */
954 if (o->cursound > -1 && o->sounddev < 0)
955 setformat(o, O_RDWR); /* need the channel, try to reopen */
956 else if (o->cursound == -1 && o->owner == NULL)
957 setformat(o, O_CLOSE); /* can close */
958 if (o->sounddev > -1) {
959 if (!o->owner) { /* no one owns the audio, so we must drain it */
960 FD_SET(o->sounddev, &rfds);
961 maxfd = MAX(o->sounddev, maxfd);
963 if (o->cursound > -1) {
964 FD_SET(o->sounddev, &wfds);
965 maxfd = MAX(o->sounddev, maxfd);
968 /* ast_select emulates linux behaviour in terms of timeout handling */
969 res = ast_select(maxfd + 1, &rfds, &wfds, NULL, NULL);
971 ast_log(LOG_WARNING, "select failed: %s\n", strerror(errno));
975 if (FD_ISSET(o->sndcmd[0], &rfds)) {
976 /* read which sound to play from the pipe */
979 read(o->sndcmd[0], &what, sizeof(what));
980 for (i = 0; sounds[i].ind != -1; i++) {
981 if (sounds[i].ind == what) {
984 o->nosound = 1; /* block audio from pbx */
988 if (sounds[i].ind == -1)
989 ast_log(LOG_WARNING, "invalid sound index: %d\n", what);
991 if (o->sounddev > -1) {
992 if (FD_ISSET(o->sounddev, &rfds)) /* read and ignore errors */
993 read(o->sounddev, ign, sizeof(ign));
994 if (FD_ISSET(o->sounddev, &wfds))
998 return NULL; /* Never reached */
1002 * reset and close the device if opened,
1003 * then open and initialize it in the desired mode,
1004 * trigger reads and writes so we can start using it.
1006 static int setformat(struct chan_usbradio_pvt *o, int mode)
1008 int fmt, desired, res, fd;
1011 if (o->sounddev >= 0) {
1012 ioctl(o->sounddev, SNDCTL_DSP_RESET, 0);
1014 o->duplex = M_UNSET;
1017 if (mode == O_CLOSE) /* we are done */
1019 if (ast_tvdiff_ms(ast_tvnow(), o->lastopen) < 1000)
1020 return -1; /* don't open too often */
1021 o->lastopen = ast_tvnow();
1022 strcpy(device, "/dev/dsp");
1024 snprintf(device + strlen("/dev/dsp"), sizeof(device) - strlen("/dev/dsp"), "%d", o->devicenum);
1025 fd = o->sounddev = open(device, mode | O_NONBLOCK);
1027 ast_log(LOG_WARNING, "Unable to re-open DSP device %d: %s\n", o->devicenum, strerror(errno));
1031 o->owner->fds[0] = fd;
1033 #if __BYTE_ORDER == __LITTLE_ENDIAN
1038 res = ioctl(fd, SNDCTL_DSP_SETFMT, &fmt);
1040 ast_log(LOG_WARNING, "Unable to set format to 16-bit signed\n");
1045 res = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
1046 /* Check to see if duplex set (FreeBSD Bug) */
1047 res = ioctl(fd, SNDCTL_DSP_GETCAPS, &fmt);
1048 if (res == 0 && (fmt & DSP_CAP_DUPLEX)) {
1049 if (option_verbose > 1)
1050 ast_verbose(VERBOSE_PREFIX_2 "Console is full duplex\n");
1055 o->duplex = M_WRITE;
1063 res = ioctl(fd, SNDCTL_DSP_STEREO, &fmt);
1065 ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
1068 fmt = desired = 48000; /* 8000 Hz desired */
1069 res = ioctl(fd, SNDCTL_DSP_SPEED, &fmt);
1072 ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
1075 if (fmt != desired) {
1076 if (!(o->warned & WARN_speed)) {
1077 ast_log(LOG_WARNING,
1078 "Requested %d Hz, got %d Hz -- sound may be choppy\n",
1080 o->warned |= WARN_speed;
1084 * on Freebsd, SETFRAGMENT does not work very well on some cards.
1085 * Default to use 256 bytes, let the user override
1089 res = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &fmt);
1091 if (!(o->warned & WARN_frag)) {
1092 ast_log(LOG_WARNING,
1093 "Unable to set fragment size -- sound may be choppy\n");
1094 o->warned |= WARN_frag;
1098 /* on some cards, we need SNDCTL_DSP_SETTRIGGER to start outputting */
1099 res = PCM_ENABLE_INPUT | PCM_ENABLE_OUTPUT;
1100 res = ioctl(fd, SNDCTL_DSP_SETTRIGGER, &res);
1101 /* it may fail if we are in half duplex, never mind */
1106 * some of the standard methods supported by channels.
1108 static int usbradio_digit_begin(struct ast_channel *c, char digit)
1113 static int usbradio_digit_end(struct ast_channel *c, char digit, unsigned int duration)
1115 /* no better use for received digits than print them */
1116 ast_verbose(" << Console Received digit %c of duration %u ms >> \n",
1121 static int usbradio_text(struct ast_channel *c, const char *text)
1123 /* print received messages */
1124 ast_verbose(" << Console Received text %s >> \n", text);
1128 /* Play ringtone 'x' on device 'o' */
1129 static void ring(struct chan_usbradio_pvt *o, int x)
1131 write(o->sndcmd[1], &x, sizeof(x));
1135 * handler for incoming calls. Either autoanswer, or start ringing
1137 static int usbradio_call(struct ast_channel *c, char *dest, int timeout)
1139 struct chan_usbradio_pvt *o = c->tech_pvt;
1141 time(&o->lasthidtime);
1142 ast_pthread_create_background(&o->hidthread, NULL, hidthread, o);
1143 ast_setstate(c, AST_STATE_UP);
1148 * remote side answered the phone
1150 static int usbradio_answer(struct ast_channel *c)
1152 struct chan_usbradio_pvt *o = c->tech_pvt;
1154 ast_setstate(c, AST_STATE_UP);
1160 static int usbradio_hangup(struct ast_channel *c)
1162 struct chan_usbradio_pvt *o = c->tech_pvt;
1168 ast_module_unref(ast_module_info->self);
1170 if (o->autoanswer || o->autohangup) {
1171 /* Assume auto-hangup too */
1173 setformat(o, O_CLOSE);
1175 /* Make congestion noise */
1176 ring(o, AST_CONTROL_CONGESTION);
1180 pthread_join(o->hidthread, NULL);
1185 /* used for data coming from the network */
1186 static int usbradio_write(struct ast_channel *c, struct ast_frame *f)
1189 struct chan_usbradio_pvt *o = c->tech_pvt;
1191 traceusb2("usbradio_write() o->nosound=%d\n", o->nosound); /*sph maw asdf */
1193 /* Immediately return if no sound is enabled */
1196 /* Stop any currently playing sound */
1199 * we could receive a block which is not a multiple of our
1200 * FRAME_SIZE, so buffer it locally and write to the device
1201 * in FRAME_SIZE chunks.
1202 * Keep the residue stored for future use.
1205 if (o->txkeyed || o->txtestkey)
1206 o->pmrChan->txPttIn = 1;
1208 o->pmrChan->txPttIn = 0;
1210 #if DEBUG_CAPTURES == 1 /* to write input data to a file datalen=320 */
1211 if (ftxcapraw && o->b.txcapraw) {
1212 i16 i, tbuff[f->datalen];
1213 for (i = 0; i < f->datalen; i += 2) {
1214 tbuff[i] = ((i16 *)(f->data))[i / 2];
1215 tbuff[i + 1] = o->txkeyed * M_Q13;
1217 fwrite(tbuff, 2, f->datalen, ftxcapraw);
1218 /*fwrite(f->data,1,f->datalen,ftxcapraw); */
1222 PmrTx(o->pmrChan,(i16*)f->data,(i16*)o->usbradio_write_buf_1);
1224 #if 0 /* to write 48KS/s stereo data to a file */
1225 if (!ftxoutraw) ftxoutraw = fopen(TX_CAP_OUT_FILE,"w");
1226 if (ftxoutraw) fwrite(o->usbradio_write_buf_1,1,f->datalen * 2 * 6,ftxoutraw);
1229 #if DEBUG_CAPTURES == 1
1230 if (o->b.txcap2 && ftxcaptrace)
1231 fwrite((o->pmrChan->ptxDebug), 1, FRAME_SIZE * 2 * 16, ftxcaptrace);
1234 src = 0; /* read position into f->data */
1235 datalen = f->datalen * 12;
1236 while (src < datalen) {
1237 /* Compute spare room in the buffer */
1238 int l = sizeof(o->usbradio_write_buf) - o->usbradio_write_dst;
1240 if (datalen - src >= l) { /* enough to fill a frame */
1241 memcpy(o->usbradio_write_buf + o->usbradio_write_dst, o->usbradio_write_buf_1 + src, l);
1242 soundcard_writeframe(o, (short *) o->usbradio_write_buf);
1244 o->usbradio_write_dst = 0;
1245 } else { /* copy residue */
1247 memcpy(o->usbradio_write_buf + o->usbradio_write_dst, o->usbradio_write_buf_1 + src, l);
1248 src += l; /* but really, we are done */
1249 o->usbradio_write_dst += l;
1255 static struct ast_frame *usbradio_read(struct ast_channel *c)
1258 struct chan_usbradio_pvt *o = c->tech_pvt;
1259 struct ast_frame *f = &o->read_f, *f1;
1260 struct ast_frame wf = { AST_FRAME_CONTROL };
1263 traceusb2("usbradio_read()\n"); /* sph maw asdf */
1265 if (o->lasthidtime) {
1267 if ((now - o->lasthidtime) > 3) {
1268 ast_log(LOG_ERROR, "HID process has died or something!!\n");
1272 if (o->lastrx && (!o->rxkeyed)) {
1274 wf.subclass = AST_CONTROL_RADIO_UNKEY;
1275 ast_queue_frame(o->owner, &wf);
1276 } else if ((!o->lastrx) && (o->rxkeyed)) {
1278 wf.subclass = AST_CONTROL_RADIO_KEY;
1279 ast_queue_frame(o->owner, &wf);
1281 /* XXX can be simplified returning &ast_null_frame */
1282 /* prepare a NULL frame in case we don't have enough data to return */
1283 memset(f, 0, sizeof(struct ast_frame));
1284 f->frametype = AST_FRAME_NULL;
1285 f->src = usbradio_tech.type;
1287 res = read(o->sounddev, o->usbradio_read_buf + o->readpos,
1288 sizeof(o->usbradio_read_buf) - o->readpos);
1289 if (res < 0) /* audio data not ready, return a NULL frame */
1293 if (o->readpos < sizeof(o->usbradio_read_buf)) /* not enough samples */
1299 #if DEBUG_CAPTURES == 1
1300 if (o->b.rxcapraw && frxcapraw)
1301 fwrite((o->usbradio_read_buf + AST_FRIENDLY_OFFSET), 1, FRAME_SIZE * 2 * 2 * 6, frxcapraw);
1306 (i16 *)(o->usbradio_read_buf + AST_FRIENDLY_OFFSET),
1307 (i16 *)(o->usbradio_read_buf_8k + AST_FRIENDLY_OFFSET));
1310 static FILE *hInput;
1311 i16 iBuff[FRAME_SIZE * 2 * 6];
1313 o->pmrChan->b.rxCapture = 1;
1316 hInput = fopen("/usr/src/xpmr/testdata/rx_in.pcm", "r");
1318 ast_log(LOG_ERROR, " Input Data File Not Found.\n");
1323 if (0 == fread((void *)iBuff, 2, FRAME_SIZE * 2 * 6, hInput))
1328 (i16 *)(o->usbradio_read_buf_8k + AST_FRIENDLY_OFFSET));
1333 if (!frxoutraw) frxoutraw = fopen(RX_CAP_OUT_FILE, "w");
1334 if (frxoutraw) fwrite((o->usbradio_read_buf_8k + AST_FRIENDLY_OFFSET), 1, FRAME_SIZE * 2, frxoutraw);
1337 #if DEBUG_CAPTURES == 1
1338 if (frxcaptrace && o->b.rxcap2) fwrite((o->pmrChan->prxDebug), 1, FRAME_SIZE * 2 * 16, frxcaptrace);
1341 if (o->rxcdtype == CD_HID && (o->pmrChan->rxExtCarrierDetect != o->rxhidsq))
1342 o->pmrChan->rxExtCarrierDetect = o->rxhidsq;
1343 if (o->rxcdtype == CD_HID_INVERT && (o->pmrChan->rxExtCarrierDetect == o->rxhidsq))
1344 o->pmrChan->rxExtCarrierDetect = !o->rxhidsq;
1346 if ( (o->rxcdtype == CD_HID && o->rxhidsq) ||
1347 (o->rxcdtype == CD_HID_INVERT && !o->rxhidsq) ||
1348 (o->rxcdtype == CD_XPMR_NOISE && o->pmrChan->rxCarrierDetect) ||
1349 (o->rxcdtype == CD_XPMR_VOX && o->pmrChan->rxCarrierDetect) )
1354 if (res != o->rxcarrierdetect) {
1355 o->rxcarrierdetect = res;
1357 ast_debug(4, "rxcarrierdetect = %d\n", res);
1360 if (o->pmrChan->rxCtcss->decode != o->rxctcssdecode) {
1362 ast_debug(4, "rxctcssdecode = %d\n", o->pmrChan->rxCtcss->decode);
1363 o->rxctcssdecode = o->pmrChan->rxCtcss->decode;
1366 if ( ( o->rxctcssfreq && (o->rxctcssdecode == o->pmrChan->rxCtcssIndex)) ||
1367 ( !o->rxctcssfreq && o->rxcarrierdetect) )
1373 o->readpos = AST_FRIENDLY_OFFSET; /* reset read pointer for next frame */
1374 if (c->_state != AST_STATE_UP) /* drop data if frame is not up */
1376 /* ok we can build and deliver the frame to the caller */
1377 f->frametype = AST_FRAME_VOICE;
1378 f->subclass = AST_FORMAT_SLINEAR;
1379 f->samples = FRAME_SIZE;
1380 f->datalen = FRAME_SIZE * 2;
1381 f->data = o->usbradio_read_buf_8k + AST_FRIENDLY_OFFSET;
1382 if (o->boost != BOOST_SCALE) { /* scale and clip values */
1384 int16_t *p = (int16_t *) f->data;
1385 for (i = 0; i < f->samples; i++) {
1386 x = (p[i] * o->boost) / BOOST_SCALE;
1389 else if (x < -32768)
1395 f->offset = AST_FRIENDLY_OFFSET;
1397 f1 = ast_dsp_process(c, o->dsp, f);
1398 if ((f1->frametype == AST_FRAME_DTMF_END) || (f1->frametype == AST_FRAME_DTMF_BEGIN)) {
1399 if ((f1->subclass == 'm') || (f1->subclass == 'u'))
1400 f1->frametype = AST_FRAME_DTMF_BEGIN;
1401 if (f1->frametype == AST_FRAME_DTMF_END)
1402 ast_log(LOG_NOTICE,"Got DTMF char %c\n",f1->subclass);
1409 static int usbradio_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
1411 struct chan_usbradio_pvt *o = newchan->tech_pvt;
1412 ast_log(LOG_WARNING,"usbradio_fixup()\n");
1417 static int usbradio_indicate(struct ast_channel *c, int cond, const void *data, size_t datalen)
1419 struct chan_usbradio_pvt *o = c->tech_pvt;
1423 case AST_CONTROL_BUSY:
1424 case AST_CONTROL_CONGESTION:
1425 case AST_CONTROL_RINGING:
1431 o->nosound = 0; /* when cursound is -1 nosound must be 0 */
1434 case AST_CONTROL_VIDUPDATE:
1437 case AST_CONTROL_HOLD:
1438 ast_verbose(" << Console Has Been Placed on Hold >> \n");
1439 ast_moh_start(c, data, o->mohinterpret);
1441 case AST_CONTROL_UNHOLD:
1442 ast_verbose(" << Console Has Been Retrieved from Hold >> \n");
1445 case AST_CONTROL_PROCEEDING:
1446 ast_verbose(" << Call Proceeding... >> \n");
1449 case AST_CONTROL_PROGRESS:
1450 ast_verbose(" << Call Progress... >> \n");
1453 case AST_CONTROL_RADIO_KEY:
1455 if(o->debuglevel)ast_verbose(" << Radio Transmit On. >> \n");
1457 case AST_CONTROL_RADIO_UNKEY:
1459 if(o->debuglevel)ast_verbose(" << Radio Transmit Off. >> \n");
1462 ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, c->name);
1473 * allocate a new channel.
1475 static struct ast_channel *usbradio_new(struct chan_usbradio_pvt *o, char *ext, char *ctx, int state)
1477 struct ast_channel *c;
1478 char device[15] = "dsp";
1481 snprintf(device + 3, sizeof(device) - 3, "%d", o->devicenum);
1482 c = ast_channel_alloc(1, state, o->cid_num, o->cid_name, "", ext, ctx, 0, "usbRadio/%s", device);
1485 c->tech = &usbradio_tech;
1486 if (o->sounddev < 0)
1487 setformat(o, O_RDWR);
1488 c->fds[0] = o->sounddev; /* -1 if device closed, override later */
1489 c->nativeformats = AST_FORMAT_SLINEAR;
1490 c->readformat = AST_FORMAT_SLINEAR;
1491 c->writeformat = AST_FORMAT_SLINEAR;
1494 if (!ast_strlen_zero(o->language))
1495 ast_string_field_set(c, language, o->language);
1496 /* Don't use ast_set_callerid() here because it will
1497 * generate a needless NewCallerID event */
1498 c->cid.cid_num = ast_strdup(o->cid_num);
1499 c->cid.cid_ani = ast_strdup(o->cid_num);
1500 c->cid.cid_name = ast_strdup(o->cid_name);
1501 if (!ast_strlen_zero(ext))
1502 c->cid.cid_dnid = ast_strdup(ext);
1505 ast_module_ref(ast_module_info->self);
1506 ast_jb_configure(c, &global_jbconf);
1507 if (state != AST_STATE_DOWN) {
1508 if (ast_pbx_start(c)) {
1509 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", c->name);
1511 o->owner = c = NULL;
1512 /* XXX what about the channel itself ? */
1513 /* XXX what about usecnt ? */
1520 static struct ast_channel *usbradio_request(const char *type, int format, void *data, int *cause)
1522 struct ast_channel *c;
1523 struct chan_usbradio_pvt *o = find_desc(data);
1525 ast_debug(4, "usbradio_request ty <%s> data 0x%p <%s>\n", type, data, (char *) data);
1527 ast_log(LOG_NOTICE, "Device %s not found\n", (char *) data);
1528 /* XXX we could default to 'dsp' perhaps ? */
1531 if ((format & AST_FORMAT_SLINEAR) == 0) {
1532 ast_log(LOG_NOTICE, "Format 0x%x unsupported\n", format);
1536 ast_log(LOG_NOTICE, "Already have a call (chan %p) on the usb channel\n", o->owner);
1537 *cause = AST_CAUSE_BUSY;
1540 c = usbradio_new(o, NULL, NULL, AST_STATE_DOWN);
1542 ast_log(LOG_WARNING, "Unable to create new usb channel\n");
1548 static char *handle_cli_radio_key(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1550 struct chan_usbradio_pvt *o = NULL;
1554 e->command = "radio key";
1556 "Usage: radio key\n"
1557 " Simulates COR active.\n";
1564 return CLI_SHOWUSAGE;
1566 o = find_desc(usbradio_active);
1572 static char *handle_cli_radio_unkey(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1574 struct chan_usbradio_pvt *o = NULL;
1578 e->command = "radio unkey";
1580 "Usage: radio unkey\n"
1581 " Simulates COR un-active.\n";
1588 return CLI_SHOWUSAGE;
1590 o = find_desc(usbradio_active);
1596 static char *handle_cli_radio_tune(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1598 struct chan_usbradio_pvt *o = NULL;
1603 e->command = "radio tune [rxnoise|rxvoice|rxtone|rxsquelch|rxcap|rxtracecap|"
1604 "txvoice|txtone|txcap|txtracecap|auxvoice|nocap|dump|save]";
1605 /* radio tune 6 3000 measured tx value */
1607 "Usage: radio tune <function>\n"
1611 " rxsquelch [newsetting]\n"
1614 " txvoice [newsetting]\n"
1615 " txtone [newsetting]\n"
1618 " auxvoice [newsetting]\n"
1621 " save (settings to tuning file)\n"
1623 " All [newsetting]s are values 0-999\n";
1629 if ((a->argc < 2) || (a->argc > 4))
1630 return CLI_SHOWUSAGE;
1632 if (a->argc == 2) { /* just show stuff */
1633 ast_cli(a->fd, "Output A is currently set to %s.\n",
1634 o->txmixa == TX_OUT_COMPOSITE ? "composite" :
1635 o->txmixa == TX_OUT_VOICE ? "voice" :
1636 o->txmixa == TX_OUT_LSD ? "tone" :
1637 o->txmixa == TX_OUT_AUX ? "auxvoice" :
1640 ast_cli(a->fd, "Output B is currently set to %s.\n",
1641 o->txmixb == TX_OUT_COMPOSITE ? "composite" :
1642 o->txmixb == TX_OUT_VOICE ? "voice" :
1643 o->txmixb == TX_OUT_LSD ? "tone" :
1644 o->txmixb == TX_OUT_AUX ? "auxvoice" :
1647 ast_cli(a->fd, "Tx Voice Level currently set to %d\n", o->txmixaset);
1648 ast_cli(a->fd, "Tx Tone Level currently set to %d\n", o->txctcssadj);
1649 ast_cli(a->fd, "Rx Squelch currently set to %d\n", o->rxsquelchadj);
1650 return CLI_SHOWUSAGE;
1653 o = find_desc(usbradio_active);
1655 if (!strcasecmp(a->argv[2], "rxnoise"))
1657 else if (!strcasecmp(a->argv[2], "rxvoice"))
1659 else if (!strcasecmp(a->argv[2], "rxtone"))
1661 else if (!strcasecmp(a->argv[2], "rxsquelch")) {
1663 ast_cli(a->fd, "Current Signal Strength is %d\n", ((32767 - o->pmrChan->rxRssi) * 1000 / 32767));
1664 ast_cli(a->fd, "Current Squelch setting is %d\n", o->rxsquelchadj);
1666 ast_cli(a->fd,"Current Raw RSSI is %d\n",o->pmrChan->rxRssi);
1667 ast_cli(a->fd,"Current (real) Squelch setting is %d\n",*(o->pmrChan->prxSquelchAdjust));
1670 i = atoi(a->argv[3]);
1671 if ((i < 0) || (i > 999))
1672 return CLI_SHOWUSAGE;
1673 ast_cli(a->fd, "Changed Squelch setting to %d\n", i);
1674 o->rxsquelchadj = i;
1675 *(o->pmrChan->prxSquelchAdjust) = ((999 - i) * 32767) / 1000;
1677 } else if (!strcasecmp(a->argv[2], "txvoice")) {
1680 if ((o->txmixa != TX_OUT_VOICE) && (o->txmixb != TX_OUT_VOICE) &&
1681 (o->txmixa != TX_OUT_COMPOSITE) && (o->txmixb != TX_OUT_COMPOSITE)) {
1682 ast_log(LOG_ERROR, "No txvoice output configured.\n");
1683 } else if (a->argc == 3) {
1684 if ((o->txmixa == TX_OUT_VOICE) || (o->txmixa == TX_OUT_COMPOSITE))
1685 ast_cli(a->fd, "Current txvoice setting on Channel A is %d\n", o->txmixaset);
1687 ast_cli(a->fd, "Current txvoice setting on Channel B is %d\n", o->txmixbset);
1689 i = atoi(a->argv[3]);
1690 if ((i < 0) || (i > 999))
1691 return CLI_SHOWUSAGE;
1693 if ((o->txmixa == TX_OUT_VOICE) || (o->txmixa == TX_OUT_COMPOSITE)) {
1695 ast_cli(a->fd, "Changed txvoice setting on Channel A to %d\n", o->txmixaset);
1698 ast_cli(a->fd, "Changed txvoice setting on Channel B to %d\n", o->txmixbset);
1702 ast_cli(a->fd, "Changed Tx Voice Output setting to %d\n", i);
1705 } else if (!strcasecmp(a->argv[2], "auxvoice")) {
1707 if ( (o->txmixa != TX_OUT_AUX) && (o->txmixb != TX_OUT_AUX))
1708 ast_log(LOG_WARNING, "No auxvoice output configured.\n");
1709 else if (a->argc == 3) {
1710 if (o->txmixa == TX_OUT_AUX)
1711 ast_cli(a->fd, "Current auxvoice setting on Channel A is %d\n", o->txmixaset);
1713 ast_cli(a->fd, "Current auxvoice setting on Channel B is %d\n", o->txmixbset);
1715 i = atoi(a->argv[3]);
1716 if ((i < 0) || (i > 999))
1717 return CLI_SHOWUSAGE;
1718 if (o->txmixa == TX_OUT_AUX) {
1720 ast_cli(a->fd, "Changed auxvoice setting on Channel A to %d\n", o->txmixaset);
1723 ast_cli(a->fd, "Changed auxvoice setting on Channel B to %d\n", o->txmixbset);
1728 /* tune_auxoutput(o,i); */
1729 } else if (!strcasecmp(a->argv[2], "txtone")) {
1731 ast_cli(a->fd, "Current Tx CTCSS modulation setting = %d\n", o->txctcssadj);
1733 i = atoi(a->argv[3]);
1734 if ((i < 0) || (i > 999))
1735 return CLI_SHOWUSAGE;
1737 set_txctcss_level(o);
1738 ast_cli(a->fd, "Changed Tx CTCSS modulation setting to %i\n", i);
1743 } else if (!strcasecmp(a->argv[2],"dump"))
1745 else if (!strcasecmp(a->argv[2],"nocap")) {
1746 ast_cli(a->fd, "File capture (trace) was rx=%d tx=%d and now off.\n", o->b.rxcap2, o->b.txcap2);
1747 ast_cli(a->fd, "File capture (raw) was rx=%d tx=%d and now off.\n", o->b.rxcapraw, o->b.txcapraw);
1748 o->b.rxcapraw = o->b.txcapraw = o->b.rxcap2 = o->b.txcap2 = o->pmrChan->b.rxCapture = o->pmrChan->b.txCapture = 0;
1754 fclose(frxcaptrace);
1766 fclose(ftxcaptrace);
1773 } else if (!strcasecmp(a->argv[2], "rxtracecap")) {
1775 frxcaptrace = fopen(RX_CAP_TRACE_FILE, "w");
1776 ast_cli(a->fd, "Trace rx on.\n");
1777 o->b.rxcap2 = o->pmrChan->b.rxCapture = 1;
1778 } else if (!strcasecmp(a->argv[2], "txtracecap")) {
1780 ftxcaptrace = fopen(TX_CAP_TRACE_FILE, "w");
1781 ast_cli(a->fd, "Trace tx on.\n");
1782 o->b.txcap2 = o->pmrChan->b.txCapture = 1;
1783 } else if (!strcasecmp(a->argv[2], "rxcap")) {
1785 frxcapraw = fopen(RX_CAP_RAW_FILE, "w");
1786 ast_cli(a->fd, "cap rx raw on.\n");
1788 } else if (!strcasecmp(a->argv[2], "txcap")) {
1790 ftxcapraw = fopen(TX_CAP_RAW_FILE, "w");
1791 ast_cli(a->fd, "cap tx raw on.\n");
1793 } else if (!strcasecmp(a->argv[2], "save")) {
1795 ast_cli(a->fd, "Saved radio tuning settings to usbradio_tune.conf\n");
1797 return CLI_SHOWUSAGE;
1802 set transmit ctcss modulation level
1803 adjust mixer output or internal gain depending on output type
1804 setting range is 0.0 to 0.9
1806 static int set_txctcss_level(struct chan_usbradio_pvt *o)
1808 if (o->txmixa == TX_OUT_LSD) {
1809 o->txmixaset = (151 * o->txctcssadj) / 1000;
1812 } else if (o->txmixb == TX_OUT_LSD) {
1813 o->txmixbset = (151 * o->txctcssadj) / 1000;
1817 *o->pmrChan->ptxCtcssAdjust = (o->txctcssadj * M_Q8) / 1000;
1822 CLI debugging on and off
1824 static char *handle_cli_radio_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1826 struct chan_usbradio_pvt *o = NULL;
1830 e->command = "radio set debug [off]";
1832 "Usage: radio set debug [off]\n"
1833 " Enable/Disable radio debugging.\n";
1837 if (a->argc < 3 || a->argc > 4)
1838 return CLI_SHOWUSAGE;
1839 if (a->argc == 4 && strncasecmp(a->argv[3], "off", 3))
1840 return CLI_SHOWUSAGE;
1842 o = find_desc(usbradio_active);
1849 ast_cli(a->fd, "USB Radio debugging %s.\n", o->debuglevel ? "enabled" : "disabled");
1854 static struct ast_cli_entry cli_usbradio[] = {
1855 AST_CLI_DEFINE(handle_cli_radio_key, "Simulate Rx Signal Present"),
1856 AST_CLI_DEFINE(handle_cli_radio_unkey, "Simulate Rx Signal Lusb"),
1857 AST_CLI_DEFINE(handle_cli_radio_tune, "Radio Tune"),
1858 AST_CLI_DEFINE(handle_cli_radio_set_debug, "Enable/Disable Radio Debugging"),
1862 * store the callerid components
1865 static void store_callerid(struct chan_usbradio_pvt *o, const char *s)
1867 ast_callerid_split(s, o->cid_name, sizeof(o->cid_name), o->cid_num, sizeof(o->cid_num));
1871 static void store_rxdemod(struct chan_usbradio_pvt *o, const char *s)
1873 if (!strcasecmp(s, "no")) {
1874 o->rxdemod = RX_AUDIO_NONE;
1875 } else if (!strcasecmp(s, "speaker")) {
1876 o->rxdemod = RX_AUDIO_SPEAKER;
1877 } else if (!strcasecmp(s, "flat")) {
1878 o->rxdemod = RX_AUDIO_FLAT;
1880 ast_log(LOG_WARNING, "Unrecognized rxdemod parameter: %s\n", s);
1883 ast_debug(4, "set rxdemod = %s\n", s);
1887 static void store_txmixa(struct chan_usbradio_pvt *o, const char *s)
1889 if (!strcasecmp(s, "no"))
1890 o->txmixa = TX_OUT_OFF;
1892 else if (!strcasecmp(s, "voice"))
1893 o->txmixa = TX_OUT_VOICE;
1894 else if (!strcasecmp(s, "tone"))
1895 o->txmixa = TX_OUT_LSD;
1896 else if (!strcasecmp(s, "composite"))
1897 o->txmixa = TX_OUT_COMPOSITE;
1898 else if (!strcasecmp(s, "auxvoice"))
1899 o->txmixb = TX_OUT_AUX;
1901 ast_log(LOG_WARNING, "Unrecognized txmixa parameter: %s\n", s);
1903 ast_debug(4, "set txmixa = %s\n", s);
1906 static void store_txmixb(struct chan_usbradio_pvt *o, const char *s)
1908 if (!strcasecmp(s, "no"))
1909 o->txmixb = TX_OUT_OFF;
1910 else if (!strcasecmp(s, "voice"))
1911 o->txmixb = TX_OUT_VOICE;
1912 else if (!strcasecmp(s, "tone"))
1913 o->txmixb = TX_OUT_LSD;
1914 else if (!strcasecmp(s, "composite"))
1915 o->txmixb = TX_OUT_COMPOSITE;
1916 else if (!strcasecmp(s, "auxvoice"))
1917 o->txmixb = TX_OUT_AUX;
1919 ast_log(LOG_WARNING, "Unrecognized txmixb parameter: %s\n", s);
1921 ast_debug(4, "set txmixb = %s\n", s);
1924 static void store_rxcdtype(struct chan_usbradio_pvt *o, const char *s)
1926 if (!strcasecmp(s, "no"))
1927 o->rxcdtype = CD_IGNORE;
1928 else if (!strcasecmp(s, "usb"))
1929 o->rxcdtype = CD_HID;
1930 else if (!strcasecmp(s, "dsp"))
1931 o->rxcdtype = CD_XPMR_NOISE;
1932 else if (!strcasecmp(s, "vox"))
1933 o->rxcdtype = CD_XPMR_VOX;
1934 else if (!strcasecmp(s, "usbinvert"))
1935 o->rxcdtype = CD_HID_INVERT;
1937 ast_log(LOG_WARNING, "Unrecognized rxcdtype parameter: %s\n", s);
1939 ast_debug(4, "set rxcdtype = %s\n", s);
1942 static void store_rxsdtype(struct chan_usbradio_pvt *o, const char *s)
1944 if (!strcasecmp(s, "no") || !strcasecmp(s, "SD_IGNORE"))
1945 o->rxsdtype = SD_IGNORE;
1946 else if (!strcasecmp(s, "usb") || !strcasecmp(s, "SD_HID"))
1947 o->rxsdtype = SD_HID;
1948 else if (!strcasecmp(s, "usbinvert") || !strcasecmp(s, "SD_HID_INVERT"))
1949 o->rxsdtype = SD_HID_INVERT;
1950 else if (!strcasecmp(s, "software") || !strcasecmp(s, "SD_XPMR"))
1951 o->rxsdtype = SD_XPMR;
1953 ast_log(LOG_WARNING, "Unrecognized rxsdtype parameter: %s\n", s);
1955 ast_debug(4, "set rxsdtype = %s\n", s);
1958 static void store_rxgain(struct chan_usbradio_pvt *o, const char *s)
1961 if (sscanf(s, "%f", &f) == 1)
1963 ast_debug(4, "set rxgain = %f\n", f);
1966 static void store_rxvoiceadj(struct chan_usbradio_pvt *o, const char *s)
1969 if (sscanf(s, "%f", &f) == 1)
1971 ast_debug(4, "set rxvoiceadj = %f\n", f);
1974 static void store_rxctcssadj(struct chan_usbradio_pvt *o, const char *s)
1977 if (sscanf(s, "%f", &f) == 1)
1979 ast_debug(4, "set rxctcssadj = %f\n", f);
1982 static void store_txtoctype(struct chan_usbradio_pvt *o, const char *s)
1984 if (!strcasecmp(s, "no") || !strcasecmp(s, "TOC_NONE"))
1985 o->txtoctype = TOC_NONE;
1986 else if (!strcasecmp(s, "phase") || !strcasecmp(s, "TOC_PHASE"))
1987 o->txtoctype = TOC_PHASE;
1988 else if (!strcasecmp(s, "notone") || !strcasecmp(s, "TOC_NOTONE"))
1989 o->txtoctype = TOC_NOTONE;
1991 ast_log(LOG_WARNING, "Unrecognized txtoctype parameter: %s\n", s);
1993 ast_debug(4, "set txtoctype = %s\n", s);
1996 static void store_rxctcssfreq(struct chan_usbradio_pvt *o, const char *s)
1999 if (sscanf(s, "%f", &f) == 1)
2001 ast_debug(4, "set rxctcss = %f\n", f);
2004 static void store_txctcssfreq(struct chan_usbradio_pvt *o, const char *s)
2007 if (sscanf(s, "%f", &f) == 1)
2009 ast_debug(4, "set txctcss = %f\n", f);
2012 static void tune_txoutput(struct chan_usbradio_pvt *o, int value)
2015 o->pmrChan->txPttIn = 1;
2018 /* generate 1KHz tone at 7200 peak */
2019 o->pmrChan->spsSigGen1->freq = 10000;
2020 o->pmrChan->spsSigGen1->outputGain = (float)(0.22 * M_Q8);
2021 o->pmrChan->b.startSpecialTone = 1;
2024 TxTestTone(o->pmrChan, 1);
2027 /* o->pmrChan->b.stopSpecialTone = 1; */
2030 TxTestTone(o->pmrChan, 0);
2032 o->pmrChan->txPttIn = 0;
2036 static void tune_rxinput(struct chan_usbradio_pvt *o)
2038 const int target = 23000;
2039 const int tolerance = 2000;
2040 const int settingmin = 1;
2041 const int settingstart = 2;
2042 const int maxtries = 12;
2046 int setting = 0, tries = 0, tmpdiscfactor, meas;
2049 settingmax = o->micmax;
2051 if (o->pmrChan->rxDemod)
2054 setting = settingstart;
2056 while (tries < maxtries) {
2057 setamixer(o->devicenum, MIXER_PARAM_MIC_CAPTURE_VOL, setting, 0);
2058 setamixer(o->devicenum, MIXER_PARAM_MIC_BOOST, o->rxboostset, 0);
2060 if (o->rxcdtype == CD_XPMR_VOX || o->rxdemod == RX_AUDIO_SPEAKER) {
2061 ast_debug(4, "Measure Direct Input\n");
2062 o->pmrChan->spsMeasure->source = o->pmrChan->spsRx->source;
2063 o->pmrChan->spsMeasure->discfactor = 1000;
2064 o->pmrChan->spsMeasure->enabled = 1;
2065 o->pmrChan->spsMeasure->amax = o->pmrChan->spsMeasure->amin = 0;
2067 meas = o->pmrChan->spsMeasure->apeak;
2068 o->pmrChan->spsMeasure->enabled = 0;
2070 ast_debug(4, "Measure HF Noise\n");
2071 tmpdiscfactor = o->pmrChan->spsRx->discfactor;
2072 o->pmrChan->spsRx->discfactor = (i16)1000;
2073 o->pmrChan->spsRx->discounteru = o->pmrChan->spsRx->discounterl = 0;
2074 o->pmrChan->spsRx->amax = o->pmrChan->spsRx->amin = 0;
2076 meas = o->pmrChan->rxRssi;
2077 o->pmrChan->spsRx->discfactor = tmpdiscfactor;
2078 o->pmrChan->spsRx->discounteru = o->pmrChan->spsRx->discounterl = 0;
2079 o->pmrChan->spsRx->amax = o->pmrChan->spsRx->amin = 0;
2083 ast_log(LOG_NOTICE, "tries=%d, setting=%d, meas=%i\n", tries, setting, meas);
2085 if ( meas < (target - tolerance) || meas > (target + tolerance) || tries < 3)
2086 setting = setting * target / meas;
2087 else if (tries > 4 && meas > (target - tolerance) && meas < (target + tolerance) )
2090 if (setting < settingmin)
2091 setting = settingmin;
2092 else if (setting > settingmax)
2093 setting = settingmax;
2097 ast_log(LOG_NOTICE, "DONE tries=%d, setting=%d, meas=%i\n", tries,
2098 (setting * 1000) / o->micmax, meas);
2099 if (meas < (target - tolerance) || meas > (target + tolerance))
2100 ast_log(LOG_NOTICE, "ERROR: RX INPUT ADJUST FAILED.\n");
2102 ast_log(LOG_NOTICE, "INFO: RX INPUT ADJUST SUCCESS.\n");
2103 o->rxmixerset = (setting * 1000) / o->micmax;
2108 static void tune_rxvoice(struct chan_usbradio_pvt *o)
2110 const int target = 7200; /* peak */
2111 const int tolerance = 360; /* peak to peak */
2112 const float settingmin = 0.1;
2113 const float settingmax = 4;
2114 const float settingstart = 1;
2115 const int maxtries = 12;
2119 int tries = 0, meas;
2121 ast_log(LOG_NOTICE, "INFO: RX VOICE ADJUST START.\n");
2122 ast_log(LOG_NOTICE, "target=%d tolerance=%d\n", target, tolerance);
2124 if (!o->pmrChan->spsMeasure)
2125 ast_log(LOG_ERROR, "NO MEASURE BLOCK.\n");
2127 if (!o->pmrChan->spsMeasure->source || !o->pmrChan->prxVoiceAdjust )
2128 ast_log(LOG_ERROR, "NO SOURCE OR MEASURE SETTING.\n");
2130 o->pmrChan->spsMeasure->source = o->pmrChan->spsRxOut->sink;
2131 o->pmrChan->spsMeasure->enabled = 1;
2132 o->pmrChan->spsMeasure->discfactor = 1000;
2134 setting=settingstart;
2136 ast_debug(4, "ERROR: NO MEASURE BLOCK.\n");
2138 while (tries < maxtries) {
2139 *(o->pmrChan->prxVoiceAdjust) = setting * M_Q8;
2141 o->pmrChan->spsMeasure->amax = o->pmrChan->spsMeasure->amin = 0;
2143 meas = o->pmrChan->spsMeasure->apeak;
2144 ast_log(LOG_NOTICE, "tries=%d, setting=%f, meas=%i\n", tries, setting, meas);
2146 if (meas < (target - tolerance) || meas > (target + tolerance) || tries < 3)
2147 setting = setting * target / meas;
2148 else if (tries > 4 && meas > (target - tolerance) && meas < (target + tolerance))
2150 if (setting < settingmin)
2151 setting = settingmin;
2152 else if (setting > settingmax)
2153 setting = settingmax;
2158 o->pmrChan->spsMeasure->enabled = 0;
2160 ast_log(LOG_NOTICE, "DONE tries=%d, setting=%f, meas=%f\n", tries, setting, (float)meas);
2161 if (meas < (target - tolerance) || meas > (target + tolerance))
2162 ast_log(LOG_ERROR, "RX VOICE GAIN ADJUST FAILED.\n");
2164 ast_log(LOG_NOTICE, "RX VOICE GAIN ADJUST SUCCESS.\n");
2165 o->rxvoiceadj = setting;
2169 static void tune_rxctcss(struct chan_usbradio_pvt *o)
2171 const int target = 4096;
2172 const int tolerance = 100;
2173 const float settingmin = 0.1;
2174 const float settingmax = 4;
2175 const float settingstart = 1;
2176 const int maxtries = 12;
2179 int tries = 0, meas;
2181 ast_log(LOG_NOTICE, "RX CTCSS ADJUST START.\n");
2182 ast_log(LOG_NOTICE, "target=%d tolerance=%d \n", target, tolerance);
2184 o->pmrChan->spsMeasure->source = o->pmrChan->prxCtcssMeasure;
2185 o->pmrChan->spsMeasure->discfactor = 400;
2186 o->pmrChan->spsMeasure->enabled = 1;
2188 setting = settingstart;
2190 while (tries < maxtries) {
2191 *(o->pmrChan->prxCtcssAdjust) = setting * M_Q8;
2193 o->pmrChan->spsMeasure->amax = o->pmrChan->spsMeasure->amin = 0;
2195 meas = o->pmrChan->spsMeasure->apeak;
2196 ast_debug(4, "tries=%d, setting=%f, meas=%i\n", tries, setting, meas);
2198 if (meas < (target - tolerance) || meas > (target + tolerance) || tries < 3)
2199 setting = setting * target / meas;
2200 else if (tries > 4 && meas > (target - tolerance) && meas < (target + tolerance))
2202 if (setting < settingmin)
2203 setting = settingmin;
2204 else if (setting > settingmax)
2205 setting = settingmax;
2209 o->pmrChan->spsMeasure->enabled = 0;
2210 ast_debug(4, "DONE tries=%d, setting=%f, meas=%f\n", tries, setting, (float)meas);
2211 if (meas < (target - tolerance) || meas > (target + tolerance))
2212 ast_log(LOG_ERROR, "RX CTCSS GAIN ADJUST FAILED.\n");
2214 ast_log(LOG_NOTICE, "RX CTCSS GAIN ADJUST SUCCESS.\n");
2215 o->rxctcssadj = setting;
2219 this file then is included in chan_usbradio.conf
2220 #include /etc/asterisk/usbradio_tune.conf
2222 static void tune_write(struct chan_usbradio_pvt *o)
2226 fp = fopen("/etc/asterisk/usbradio_tune.conf", "w");
2228 if (!strcmp(o->name, "dsp"))
2229 fprintf(fp, "[general]\n");
2231 fprintf(fp, "[%s]\n", o->name);
2233 fprintf(fp, "; name=%s\n", o->name);
2234 fprintf(fp, "; devicenum=%d\n", o->devicenum);
2236 fprintf(fp, "rxmixerset=%d\n", o->rxmixerset);
2237 fprintf(fp, "rxboostset=%d\n", o->rxboostset);
2238 fprintf(fp, "txmixaset=%d\n", o->txmixaset);
2239 fprintf(fp, "txmixbset=%d\n", o->txmixbset);
2241 fprintf(fp, "rxvoiceadj=%f\n", o->rxvoiceadj);
2242 fprintf(fp, "rxctcssadj=%f\n", o->rxctcssadj);
2243 fprintf(fp, "txctcssadj=%d\n", o->txctcssadj);
2245 fprintf(fp, "rxsquelchadj=%d\n", o->rxsquelchadj);
2249 static void mixer_write(struct chan_usbradio_pvt *o)
2251 setamixer(o->devicenum, MIXER_PARAM_MIC_PLAYBACK_SW, 0, 0);
2252 setamixer(o->devicenum, MIXER_PARAM_MIC_PLAYBACK_VOL, 0, 0);
2253 setamixer(o->devicenum, MIXER_PARAM_SPKR_PLAYBACK_SW, 1, 0);
2254 setamixer(o->devicenum, MIXER_PARAM_SPKR_PLAYBACK_VOL,
2255 o->txmixaset * o->spkrmax / 1000,
2256 o->txmixbset * o->spkrmax / 1000);
2257 setamixer(o->devicenum, MIXER_PARAM_MIC_CAPTURE_VOL,
2258 o->rxmixerset * o->micmax / 1000, 0);
2259 setamixer(o->devicenum, MIXER_PARAM_MIC_BOOST, o->rxboostset, 0);
2260 setamixer(o->devicenum, MIXER_PARAM_MIC_CAPTURE_SW, 1, 0);
2263 adjust dsp multiplier to add resolution to tx level adjustment
2265 static void mult_set(struct chan_usbradio_pvt *o)
2268 if (o->pmrChan->spsTxOutA) {
2269 o->pmrChan->spsTxOutA->outputGain =
2270 mult_calc((o->txmixaset * 152) / 1000);
2272 if (o->pmrChan->spsTxOutB) {
2273 o->pmrChan->spsTxOutB->outputGain =
2274 mult_calc((o->txmixbset * 152) / 1000);
2278 * input 0 - 151 outputs are pot and multiplier
2280 static int mult_calc(int value)
2282 const int multx = M_Q8;
2285 pot= ((int)(value / 4) * 4) + 2;
2286 mult = multx - ((multx * (3 - (value % 4))) / (pot + 2));
2290 #define pd(x) ast_debug(4, #x" = %d\n", x)
2291 #define pp(x) ast_debug(4, #x" = %p\n", x)
2292 #define ps(x) ast_debug(4, #x" = %s\n", x)
2293 #define pf(x) ast_debug(4, #x" = %f\n", x)
2296 static void pmrdump(struct chan_usbradio_pvt *o)
2302 ast_debug(4, "odump()\n");
2314 pd(o->rxsquelchadj);
2323 ast_debug(4, "pmrdump()\n");
2325 ast_debug(4, "prxSquelchAdjust=%d\n", *(o->pmrChan->prxSquelchAdjust));
2327 pd(p->rxCarrierPoint);
2328 pd(p->rxCarrierHyst);
2330 pd(p->rxCtcss->relax);
2332 pd(p->rxCtcssIndex);
2338 pd(p->rxDeEmpEnable);
2339 pd(p->rxCenterSlicerEnable);
2340 pd(p->rxCtcssDecodeEnable);
2341 pd(p->rxDcsDecodeEnable);
2344 pd(p->txLimiterEnable);
2345 pd(p->txPreEmpEnable);
2349 pd(p->spsTxOutA->outputGain);
2351 pd(p->spsTxOutB->outputGain);
2358 * grab fields from the config file, init the descriptor and open the device.
2360 static struct chan_usbradio_pvt *store_config(struct ast_config *cfg, char *ctg)
2362 struct ast_variable *v;
2363 struct chan_usbradio_pvt *o;
2364 struct ast_config *cfg1;
2365 struct ast_flags config_flags = { 0 };
2368 traceusb1(" store_config() ctg == NULL\n");
2369 o = &usbradio_default;
2372 if (!(o = ast_calloc(1, sizeof(*o)))){
2375 *o = usbradio_default;
2376 /* "general" is also the default thing */
2377 if (strcmp(ctg, "general") == 0) {
2378 o->name = ast_strdup("dsp");
2379 usbradio_active = o->name;
2381 o->name = ast_strdup(ctg);
2384 strcpy(o->mohinterpret, "default");
2385 o->micmax = amixer_max(o->devicenum, MIXER_PARAM_MIC_CAPTURE_VOL);
2386 o->spkrmax = amixer_max(o->devicenum, MIXER_PARAM_SPKR_PLAYBACK_VOL);
2387 /* fill other fields from configuration */
2388 for (v = ast_variable_browse(cfg, ctg); v; v = v->next) {
2390 /* handle jb conf */
2391 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
2393 CV_START(v->name, v->value);
2395 CV_UINT("frags", o->frags);
2396 CV_UINT("queuesize", o->queuesize);
2397 CV_UINT("devicenum", o->devicenum);
2398 CV_UINT("debug", usbradio_debug);
2399 CV_BOOL("rxcpusaver", o->rxcpusaver);
2400 CV_BOOL("txcpusaver", o->txcpusaver);
2401 CV_BOOL("invertptt", o->invertptt);
2402 CV_F("rxdemod", store_rxdemod(o, v->value));
2403 CV_BOOL("txprelim", o->txprelim);;
2404 CV_F("txmixa", store_txmixa(o, v->value));
2405 CV_F("txmixb", store_txmixb(o, v->value));
2406 CV_F("carrierfrom", store_rxcdtype(o, v->value));
2407 CV_F("rxsdtype", store_rxsdtype(o, v->value));
2408 CV_F("rxctcssfreq", store_rxctcssfreq(o, v->value));
2409 CV_F("txctcssfreq", store_txctcssfreq(o, v->value));
2410 CV_F("rxgain", store_rxgain(o, v->value));
2411 CV_BOOL("rxboostset", o->rxboostset);
2412 CV_UINT("rxctcssrelax", o->rxctcssrelax);
2413 CV_F("txtoctype", store_txtoctype(o, v->value));
2414 CV_UINT("hdwtype", o->hdwtype);
2415 CV_UINT("duplex", o->radioduplex);
2420 cfg1 = ast_config_load(config1, config_flags);
2422 o->rxmixerset = 500;
2425 o->rxvoiceadj = 0.5;
2426 o->rxctcssadj = 0.5;
2427 o->txctcssadj = 200;
2428 o->rxsquelchadj = 500;
2429 ast_log(LOG_WARNING, "File %s not found, using default parameters.\n", config1);
2431 for (v = ast_variable_browse(cfg1, ctg); v; v = v->next) {
2433 CV_START(v->name, v->value);
2434 CV_UINT("rxmixerset", o->rxmixerset);
2435 CV_UINT("txmixaset", o->txmixaset);
2436 CV_UINT("txmixbset", o->txmixbset);
2437 CV_F("rxvoiceadj", store_rxvoiceadj(o, v->value));
2438 CV_F("rxctcssadj", store_rxctcssadj(o, v->value));
2439 CV_UINT("txctcssadj", o->txctcssadj);
2440 CV_UINT("rxsquelchadj", o->rxsquelchadj);
2443 ast_config_destroy(cfg1);
2448 if (o == &usbradio_default) /* we are done with the default */
2451 o->lastopen = ast_tvnow(); /* don't leave it 0 or tvdiff may wrap */
2452 o->dsp = ast_dsp_new();
2454 ast_dsp_set_features(o->dsp, DSP_FEATURE_DTMF_DETECT);
2455 ast_dsp_digitmode(o->dsp, DSP_DIGITMODE_DTMF | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_RELAXDTMF);
2458 if (o->rxctcssfreq != 0 && o->rxdemod == RX_AUDIO_SPEAKER)
2459 ast_log(LOG_ERROR, "Incompatable Options o->rxctcssfreq=%f and o->rxdemod=speaker\n", o->rxctcssfreq);
2461 if (o->pmrChan == NULL) {
2464 memset(&tChan, 0, sizeof(tChan));
2466 tChan.rxDemod = o->rxdemod;
2467 tChan.rxCdType = o->rxcdtype;
2469 tChan.txMod = o->txprelim;
2471 tChan.txMixA = o->txmixa;
2472 tChan.txMixB = o->txmixb;
2474 tChan.rxCpuSaver = o->rxcpusaver;
2475 tChan.txCpuSaver = o->txcpusaver;
2477 tChan.rxCtcssFreq = o->rxctcssfreq;
2478 tChan.txCtcssFreq = o->txctcssfreq;
2480 o->pmrChan = createPmrChannel(&tChan, FRAME_SIZE);
2482 o->pmrChan->radioDuplex = o->radioduplex;
2484 o->pmrChan->rxCpuSaver = o->rxcpusaver;
2485 o->pmrChan->txCpuSaver = o->txcpusaver;
2487 *(o->pmrChan->prxSquelchAdjust) =
2488 ((999 - o->rxsquelchadj) * 32767) / 1000;
2490 o->pmrChan->spsRx->outputGain = o->rxvoiceadj*M_Q8;
2492 o->pmrChan->txTocType = o->txtoctype;
2494 if ((o->txmixa == TX_OUT_LSD) ||
2495 (o->txmixa == TX_OUT_COMPOSITE) ||
2496 (o->txmixb == TX_OUT_LSD) ||
2497 (o->txmixb == TX_OUT_COMPOSITE)) {
2498 *(o->pmrChan->prxCtcssAdjust) = o->rxctcssadj * M_Q8;
2499 set_txctcss_level(o);
2502 o->pmrChan->rxCtcss->relax = o->rxctcssrelax;
2506 if ((o->txmixa != TX_OUT_VOICE) && (o->txmixb != TX_OUT_VOICE) &&
2507 (o->txmixa != TX_OUT_COMPOSITE) && (o->txmixb != TX_OUT_COMPOSITE))
2508 ast_log(LOG_ERROR, "No txvoice output configured.\n");
2510 if (o->txctcssfreq &&
2511 o->txmixa != TX_OUT_LSD && o->txmixa != TX_OUT_COMPOSITE &&
2512 o->txmixb != TX_OUT_LSD && o->txmixb != TX_OUT_COMPOSITE)
2513 ast_log(LOG_ERROR, "No txtone output configured.\n");
2515 if (o->rxctcssfreq && o->pmrChan->rxCtcssIndex < 0)
2516 ast_log(LOG_ERROR, "Invalid CTCSS Frequency.\n");
2526 if (pipe(o->sndcmd) != 0) {
2527 ast_log(LOG_ERROR, "Unable to create pipe\n");
2531 ast_debug(4, "creating sound thread\n");
2532 ast_pthread_create_background(&o->sthread, NULL, sound_thread, o);
2534 /* link into list of devices */
2535 if (o != &usbradio_default) {
2536 o->next = usbradio_default.next;
2537 usbradio_default.next = o;
2542 if (o != &usbradio_default)
2547 #if DEBUG_FILETEST == 1
2551 int RxTestIt(struct chan_usbradio_pvt *o)
2553 const int numSamples = SAMPLES_PER_BLOCK;
2554 const int numChannels = 16;
2565 FILE *hInput = NULL, *hOutput = NULL, *hOutputTx = NULL;
2567 i16 iBuff[numSamples * 2 * 6], oBuff[numSamples];
2569 ast_debug(4, "RxTestIt()\n");
2572 pChan->b.txCapture = 1;
2573 pChan->b.rxCapture = 1;
2577 hInput = fopen("/usr/src/xpmr/testdata/rx_in.pcm", "r");
2579 ast_debug(4, " RxTestIt() File Not Found.\n");
2582 hOutput = fopen("/usr/src/xpmr/testdata/rx_debug.pcm", "w");
2584 ast_debug(4, " RxTestIt() Working...\n");
2586 while (!feof(hInput)) {
2587 fread((void *)iBuff, 2, numSamples * 2 * 6, hInput);
2590 txHangTime -= numSamples;
2594 if (pChan->rxCtcss->decode)
2595 txHangTime = (8000 / 1000 * 2000);
2597 if (pChan->rxCtcss->decode && !txEnable) {
2599 /* pChan->inputBlanking = (8000 / 1000 * 200); */
2600 } else if (!pChan->rxCtcss->decode && txEnable) {
2604 PmrRx(pChan, iBuff, oBuff);
2606 fwrite((void *)pChan->prxDebug, 2, numSamples * numChannels, hOutput);
2608 pChan->b.txCapture = 0;
2609 pChan->b.rxCapture = 0;
2616 ast_debug(4, " RxTestIt() Complete.\n");
2622 #include "./xpmr/xpmr.c"
2625 static int load_module(void)
2627 struct ast_config *cfg = NULL;
2629 struct ast_flags config_flags = { 0 };
2631 /* Copy the default jb config over global_jbconf */
2632 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
2634 /* load config file */
2635 if (!(cfg = ast_config_load(config, config_flags))) {
2636 ast_log(LOG_NOTICE, "Unable to load config %s\n", config);
2637 return AST_MODULE_LOAD_DECLINE;
2641 store_config(cfg, ctg);
2642 } while ( (ctg = ast_category_browse(cfg, ctg)) != NULL);
2644 ast_config_destroy(cfg);
2646 if (find_desc(usbradio_active) == NULL) {
2647 ast_log(LOG_NOTICE, "Device %s not found\n", usbradio_active);
2648 /* XXX we could default to 'dsp' perhaps ? */
2649 /* XXX should cleanup allocated memory etc. */
2650 return AST_MODULE_LOAD_FAILURE;
2653 if (ast_channel_register(&usbradio_tech)) {
2654 ast_log(LOG_ERROR, "Unable to register channel type 'usb'\n");
2655 return AST_MODULE_LOAD_FAILURE;
2658 ast_cli_register_multiple(cli_usbradio, sizeof(cli_usbradio) / sizeof(struct ast_cli_entry));
2660 return AST_MODULE_LOAD_SUCCESS;
2664 static int unload_module(void)
2666 struct chan_usbradio_pvt *o;
2668 ast_log(LOG_WARNING, "unload_module() called\n");
2670 ast_channel_unregister(&usbradio_tech);
2671 ast_cli_unregister_multiple(cli_usbradio, sizeof(cli_usbradio) / sizeof(struct ast_cli_entry));
2673 for (o = usbradio_default.next; o; o = o->next) {
2675 ast_log(LOG_WARNING, "destroyPmrChannel() called\n");
2677 destroyPmrChannel(o->pmrChan);
2679 #if DEBUG_CAPTURES == 1
2680 if (frxcapraw) { fclose(frxcapraw); frxcapraw = NULL; }
2681 if (frxcaptrace) { fclose(frxcaptrace); frxcaptrace = NULL; }
2682 if (frxoutraw) { fclose(frxoutraw); frxoutraw = NULL; }
2683 if (ftxcapraw) { fclose(ftxcapraw); ftxcapraw = NULL; }
2684 if (ftxcaptrace) { fclose(ftxcaptrace); ftxcaptrace = NULL; }
2685 if (ftxoutraw) { fclose(ftxoutraw); ftxoutraw = NULL; }
2689 if (o->sndcmd[0] > 0) {
2690 close(o->sndcmd[0]);
2691 close(o->sndcmd[1]);
2694 ast_dsp_free(o->dsp);
2696 ast_softhangup(o->owner, AST_SOFTHANGUP_APPUNLOAD);
2697 if (o->owner) /* XXX how ??? */
2699 /* XXX what about the thread ? */
2700 /* XXX what about the memory allocated ? */
2705 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "usb Console Channel Driver");