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 #define C108_VENDOR_ID 0x0d8c
118 #define C108_PRODUCT_ID 0x000c
119 #define C108_HID_INTERFACE 3
121 #define HID_REPORT_GET 0x01
122 #define HID_REPORT_SET 0x09
124 #define HID_RT_INPUT 0x01
125 #define HID_RT_OUTPUT 0x02
127 /*! Global jitterbuffer configuration - by default, jb is disabled */
128 static struct ast_jb_conf default_jbconf =
132 .resync_threshold = -1,
135 static struct ast_jb_conf global_jbconf;
138 * usbradio.conf parameters are
142 ; General config options, with default values shown.
143 ; You should use one section per device, with [general] being used
147 ; debug = 0x0 ; misc debug flags, default is 0
149 ; Set the device to use for I/O
151 ; Set hardware type here
152 ; hdwtype=0 ; 0=limey, 1=sph
154 ; rxboostset=0 ; no rx gain boost
155 ; rxctcssrelax=1 ; reduce talkoff from radios w/o CTCSS Tx HPF
156 ; rxctcssfreq=100.0 ; rx ctcss freq in floating point. must be in table
157 ; txctcssfreq=100.0 ; tx ctcss freq, any frequency permitted
159 ; carrierfrom=dsp ;no,usb,usbinvert,dsp,vox
160 ; ctcssfrom=dsp ;no,usb,dsp
162 ; rxdemod=flat ; input type from radio: no,speaker,flat
163 ; txprelim=yes ; output is pre-emphasised and limited
164 ; txtoctype=no ; no,phase,notone
166 ; txmixa=composite ;no,voice,tone,composite,auxvoice
167 ; txmixb=no ;no,voice,tone,composite,auxvoice
171 ;------------------------------ JITTER BUFFER CONFIGURATION --------------------------
172 ; jbenable = yes ; Enables the use of a jitterbuffer on the receiving side of an
173 ; USBRADIO channel. Defaults to "no". An enabled jitterbuffer will
174 ; be used only if the sending side can create and the receiving
175 ; side can not accept jitter. The USBRADIO channel can't accept jitter,
176 ; thus an enabled jitterbuffer on the receive USBRADIO side will always
177 ; be used if the sending side can create jitter.
179 ; jbmaxsize = 200 ; Max length of the jitterbuffer in milliseconds.
181 ; jbresyncthreshold = 1000 ; Jump in the frame timestamps over which the jitterbuffer is
182 ; resynchronized. Useful to improve the quality of the voice, with
183 ; big jumps in/broken timestamps, usualy sent from exotic devices
184 ; and programs. Defaults to 1000.
186 ; jbimpl = fixed ; Jitterbuffer implementation, used on the receiving side of an USBRADIO
187 ; channel. Two implementations are currenlty available - "fixed"
188 ; (with size always equals to jbmax-size) and "adaptive" (with
189 ; variable size, actually the new jb of IAX2). Defaults to fixed.
191 ; jblog = no ; Enables jitterbuffer frame logging. Defaults to "no".
192 ;-----------------------------------------------------------------------------------
200 * The following parameters are used in the driver:
202 * FRAME_SIZE the size of an audio frame, in samples.
203 * 160 is used almost universally, so you should not change it.
205 * FRAGS the argument for the SETFRAGMENT ioctl.
206 * Overridden by the 'frags' parameter in usbradio.conf
208 * Bits 0-7 are the base-2 log of the device's block size,
209 * bits 16-31 are the number of blocks in the driver's queue.
210 * There are a lot of differences in the way this parameter
211 * is supported by different drivers, so you may need to
212 * experiment a bit with the value.
213 * A good default for linux is 30 blocks of 64 bytes, which
214 * results in 6 frames of 320 bytes (160 samples).
215 * FreeBSD works decently with blocks of 256 or 512 bytes,
216 * leaving the number unspecified.
217 * Note that this only refers to the device buffer size,
218 * this module will then try to keep the lenght of audio
219 * buffered within small constraints.
221 * QUEUE_SIZE The max number of blocks actually allowed in the device
222 * driver's buffer, irrespective of the available number.
223 * Overridden by the 'queuesize' parameter in usbradio.conf
225 * Should be >=2, and at most as large as the hw queue above
226 * (otherwise it will never be full).
229 #define FRAME_SIZE 160
230 #define QUEUE_SIZE 20
232 #if defined(__FreeBSD__)
235 #define FRAGS ( ( (6 * 5) << 16 ) | 0xc )
239 * XXX text message sizes are probably 256 chars, but i am
240 * not sure if there is a suitable definition anywhere.
242 #define TEXT_SIZE 256
245 #define TRYOPEN 1 /* try to open on startup */
247 #define O_CLOSE 0x444 /* special 'close' mode for device */
248 /* Which device to use */
249 #if defined( __OpenBSD__ ) || defined( __NetBSD__ )
250 #define DEV_DSP "/dev/audio"
252 #define DEV_DSP "/dev/dsp"
256 #define MIN(a,b) ((a) < (b) ? (a) : (b))
259 #define MAX(a,b) ((a) > (b) ? (a) : (b))
262 static char *config = "usbradio.conf"; /* default config file */
263 static char *config1 = "usbradio_tune.conf"; /* tune config file */
265 static FILE *frxcapraw = NULL, *frxcaptrace = NULL, *frxoutraw = NULL;
266 static FILE *ftxcapraw = NULL, *ftxcaptrace = NULL, *ftxoutraw = NULL;
268 static int usbradio_debug;
269 #if 0 /* maw asdf sph */
270 static int usbradio_debug_level = 0;
273 enum {RX_AUDIO_NONE,RX_AUDIO_SPEAKER,RX_AUDIO_FLAT};
274 enum {CD_IGNORE,CD_XPMR_NOISE,CD_XPMR_VOX,CD_HID,CD_HID_INVERT};
275 enum {SD_IGNORE,SD_HID,SD_HID_INVERT,SD_XPMR}; /* no,external,externalinvert,software */
276 enum {RX_KEY_CARRIER,RX_KEY_CARRIER_CODE};
277 enum {TX_OUT_OFF,TX_OUT_VOICE,TX_OUT_LSD,TX_OUT_COMPOSITE,TX_OUT_AUX};
278 enum {TOC_NONE,TOC_PHASE,TOC_NOTONE};
280 /* DECLARE STRUCTURES */
283 * descriptor for one of our channels.
284 * There is one used for 'default' values (from the [general] entry in
285 * the configuration file), and then one instance for each device
286 * (the default is cloned from [general], others are only created
287 * if the relevant section exists).
289 struct chan_usbradio_pvt {
290 struct chan_usbradio_pvt *next;
294 int total_blocks; /* total blocks in the output device */
296 enum { M_UNSET, M_FULL, M_READ, M_WRITE } duplex;
301 unsigned int queuesize; /* max fragments in queue */
302 unsigned int frags; /* parameter for SETFRAGMENT */
304 int warned; /* various flags used for warnings */
305 #define WARN_used_blocks 1
308 int w_errors; /* overfull in the write path */
309 struct timeval lastopen;
314 /* boost support. BOOST_SCALE * 10 ^(BOOST_MAX/20) must
315 * be representable in 16 bits to avoid overflows.
317 #define BOOST_SCALE (1<<9)
318 #define BOOST_MAX 40 /* slightly less than 7 bits */
319 int boost; /* input boost, scaled by BOOST_SCALE */
328 struct ast_channel *owner;
329 char ext[AST_MAX_EXTENSION];
330 char ctx[AST_MAX_CONTEXT];
331 char language[MAX_LANGUAGE];
332 char cid_name[256]; /* XXX */
333 char cid_num[256]; /* XXX */
334 char mohinterpret[MAX_MUSICCLASS];
336 /* buffers used in usbradio_write, 2 per int by 2 channels by 6 times oversampling (48KS/s) */
337 char usbradio_write_buf[FRAME_SIZE * 2 * 2 * 6];
338 char usbradio_write_buf_1[FRAME_SIZE * 2 * 2 * 6];
340 int usbradio_write_dst;
341 /* buffers used in usbradio_read - AST_FRIENDLY_OFFSET space for headers
342 * plus enough room for a full frame
344 char usbradio_read_buf[FRAME_SIZE * (2 * 12) + AST_FRIENDLY_OFFSET];
345 char usbradio_read_buf_8k[FRAME_SIZE * 2 + AST_FRIENDLY_OFFSET];
346 int readpos; /* read position above */
347 struct ast_frame read_f; /* returned by usbradio_read */
355 char rxcarrierdetect; /*!< status from pmr channel */
356 char rxctcssdecode; /*!< status from pmr channel */
359 char rxkeyed; /*!< indicates rx signal present */
362 char txkeyed; /*! tx key request from upper layers */
378 int rxsquelchadj; /*!< this copy needs to be here for initialization */
403 int hid_gpio_ctl_loc;
407 int hid_io_ctcss_loc;
419 /* maw add additional defaults !!! */
420 static struct chan_usbradio_pvt usbradio_default = {
422 .duplex = M_UNSET, /* XXX check this */
425 .queuesize = QUEUE_SIZE,
429 .readpos = AST_FRIENDLY_OFFSET, /* start here on reads */
430 .lastopen = { 0, 0 },
431 .boost = BOOST_SCALE,
434 /* DECLARE FUNCTION PROTOTYPES */
436 static void store_txtoctype(struct chan_usbradio_pvt *o, const char *s);
437 static int hidhdwconfig(struct chan_usbradio_pvt *o);
438 static int set_txctcss_level(struct chan_usbradio_pvt *o);
439 static void pmrdump(struct chan_usbradio_pvt *o);
440 static void mult_set(struct chan_usbradio_pvt *o);
441 static int mult_calc(int value);
442 static void mixer_write(struct chan_usbradio_pvt *o);
443 static void tune_rxinput(struct chan_usbradio_pvt *o);
444 static void tune_rxvoice(struct chan_usbradio_pvt *o);
445 static void tune_rxctcss(struct chan_usbradio_pvt *o);
446 static void tune_txoutput(struct chan_usbradio_pvt *o, int value);
447 static void tune_write(struct chan_usbradio_pvt *o);
449 static char *usbradio_active; /* the active device */
451 static int setformat(struct chan_usbradio_pvt *o, int mode);
453 static struct ast_channel *usbradio_request(const char *type, int format, void *data
455 static int usbradio_digit_begin(struct ast_channel *c, char digit);
456 static int usbradio_digit_end(struct ast_channel *c, char digit, unsigned int duration);
457 static int usbradio_text(struct ast_channel *c, const char *text);
458 static int usbradio_hangup(struct ast_channel *c);
459 static int usbradio_answer(struct ast_channel *c);
460 static struct ast_frame *usbradio_read(struct ast_channel *chan);
461 static int usbradio_call(struct ast_channel *c, char *dest, int timeout);
462 static int usbradio_write(struct ast_channel *chan, struct ast_frame *f);
463 static int usbradio_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen);
464 static int usbradio_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
466 #if DEBUG_FILETEST == 1
467 static int RxTestIt(struct chan_usbradio_pvt *o);
470 static char tdesc[] = "USB (CM108) Radio Channel Driver";
472 static const struct ast_channel_tech usbradio_tech = {
474 .description = tdesc,
475 .capabilities = AST_FORMAT_SLINEAR,
476 .requester = usbradio_request,
477 .send_digit_begin = usbradio_digit_begin,
478 .send_digit_end = usbradio_digit_end,
479 .send_text = usbradio_text,
480 .hangup = usbradio_hangup,
481 .answer = usbradio_answer,
482 .read = usbradio_read,
483 .call = usbradio_call,
484 .write = usbradio_write,
485 .indicate = usbradio_indicate,
486 .fixup = usbradio_fixup,
489 /* Call with: devnum: alsa major device number, param: ascii Formal
490 Parameter Name, val1, first or only value, val2 second value, or 0
491 if only 1 value. Values: 0-99 (percent) or 0-1 for baboon.
493 Note: must add -lasound to end of linkage */
495 static int amixer_max(int devnum,char *param)
500 snd_ctl_elem_id_t *id;
501 snd_hctl_elem_t *elem;
502 snd_ctl_elem_info_t *info;
504 snprintf(str, sizeof(str), "hw:%d", devnum);
505 if (snd_hctl_open(&hctl, str, 0))
508 id = alloca(snd_ctl_elem_id_sizeof());
509 memset(id, 0, snd_ctl_elem_id_sizeof());
510 snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
511 snd_ctl_elem_id_set_name(id, param);
512 elem = snd_hctl_find_elem(hctl, id);
514 snd_hctl_close(hctl);
517 info = alloca(snd_ctl_elem_info_sizeof());
518 memset(info, 0, snd_ctl_elem_info_sizeof());
519 snd_hctl_elem_info(elem,info);
520 type = snd_ctl_elem_info_get_type(info);
523 case SND_CTL_ELEM_TYPE_INTEGER:
524 rv = snd_ctl_elem_info_get_max(info);
526 case SND_CTL_ELEM_TYPE_BOOLEAN:
530 snd_hctl_close(hctl);
534 /*! \brief Call with: devnum: alsa major device number, param: ascii Formal
535 Parameter Name, val1, first or only value, val2 second value, or 0
536 if only 1 value. Values: 0-99 (percent) or 0-1 for baboon.
538 Note: must add -lasound to end of linkage */
540 static int setamixer(int devnum, char *param, int v1, int v2)
545 snd_ctl_elem_id_t *id;
546 snd_ctl_elem_value_t *control;
547 snd_hctl_elem_t *elem;
548 snd_ctl_elem_info_t *info;
550 snprintf(str, sizeof(str), "hw:%d", devnum);
551 if (snd_hctl_open(&hctl, str, 0))
554 id = alloca(snd_ctl_elem_id_sizeof());
555 memset(id, 0, snd_ctl_elem_id_sizeof());
556 snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
557 snd_ctl_elem_id_set_name(id, param);
558 elem = snd_hctl_find_elem(hctl, id);
560 snd_hctl_close(hctl);
563 info = alloca(snd_ctl_elem_info_sizeof());
564 memset(info, 0, snd_ctl_elem_info_sizeof());
565 snd_hctl_elem_info(elem,info);
566 type = snd_ctl_elem_info_get_type(info);
567 control = alloca(snd_ctl_elem_value_sizeof());
568 memset(control, 0, snd_ctl_elem_value_sizeof());
569 snd_ctl_elem_value_set_id(control, id);
571 case SND_CTL_ELEM_TYPE_INTEGER:
572 snd_ctl_elem_value_set_integer(control, 0, v1);
573 if (v2 > 0) snd_ctl_elem_value_set_integer(control, 1, v2);
575 case SND_CTL_ELEM_TYPE_BOOLEAN:
576 snd_ctl_elem_value_set_integer(control, 0, (v1 != 0));
579 if (snd_hctl_elem_write(elem, control)) {
580 snd_hctl_close(hctl);
583 snd_hctl_close(hctl);
587 static void hid_set_outputs(struct usb_dev_handle *handle,
588 unsigned char *outputs)
590 usb_control_msg(handle,
591 USB_ENDPOINT_OUT + USB_TYPE_CLASS + USB_RECIP_INTERFACE,
593 0 + (HID_RT_OUTPUT << 8),
595 (char *)outputs, 4, 5000);
598 static void hid_get_inputs(struct usb_dev_handle *handle,
599 unsigned char *inputs)
601 usb_control_msg(handle,
602 USB_ENDPOINT_IN + USB_TYPE_CLASS + USB_RECIP_INTERFACE,
604 0 + (HID_RT_INPUT << 8),
606 (char *)inputs, 4, 5000);
609 static struct usb_device *hid_device_init(void)
611 struct usb_bus *usb_bus;
612 struct usb_device *dev;
617 for (usb_bus = usb_busses; usb_bus; usb_bus = usb_bus->next) {
618 for (dev = usb_bus->devices; dev; dev = dev->next) {
619 if ((dev->descriptor.idVendor == C108_VENDOR_ID) && (dev->descriptor.idProduct == C108_PRODUCT_ID))
626 static int hidhdwconfig(struct chan_usbradio_pvt *o)
628 if (o->hdwtype == 1) { /*sphusb */
629 o->hid_gpio_ctl = 0x08; /* set GPIO4 to output mode */
630 o->hid_gpio_ctl_loc = 2; /* For CTL of GPIO */
631 o->hid_io_cor = 4; /* GPIO3 is COR */
632 o->hid_io_cor_loc = 1; /* GPIO3 is COR */
633 o->hid_io_ctcss = 2; /* GPIO 2 is External CTCSS */
634 o->hid_io_ctcss_loc = 1; /* is GPIO 2 */
635 o->hid_io_ptt = 8; /* GPIO 4 is PTT */
636 o->hid_gpio_loc = 1; /* For ALL GPIO */
637 } else if (o->hdwtype == 0) { /* dudeusb */
638 o->hid_gpio_ctl = 0x0c;/* set GPIO 3 & 4 to output mode */
639 o->hid_gpio_ctl_loc = 2; /* For CTL of GPIO */
640 o->hid_io_cor = 2; /* VOLD DN is COR */
641 o->hid_io_cor_loc = 0; /* VOL DN COR */
642 o->hid_io_ctcss = 2; /* GPIO 2 is External CTCSS */
643 o->hid_io_ctcss_loc = 1; /* is GPIO 2 */
644 o->hid_io_ptt = 4; /* GPIO 3 is PTT */
645 o->hid_gpio_loc = 1; /* For ALL GPIO */
646 } else if (o->hdwtype == 3) { /* custom version */
647 o->hid_gpio_ctl = 0x0c; /* set GPIO 3 & 4 to output mode */
648 o->hid_gpio_ctl_loc = 2; /* For CTL of GPIO */
649 o->hid_io_cor = 2; /* VOLD DN is COR */
650 o->hid_io_cor_loc = 0; /* VOL DN COR */
651 o->hid_io_ctcss = 2; /* GPIO 2 is External CTCSS */
652 o->hid_io_ctcss_loc = 1; /* is GPIO 2 */
653 o->hid_io_ptt = 4; /* GPIO 3 is PTT */
654 o->hid_gpio_loc = 1; /* For ALL GPIO */
661 static void *hidthread(void *arg)
663 unsigned char buf[4], keyed;
665 struct usb_device *usb_dev;
666 struct usb_dev_handle *usb_handle;
667 struct chan_usbradio_pvt *o = arg;
669 usb_dev = hid_device_init();
670 if (usb_dev == NULL) {
671 ast_log(LOG_ERROR, "USB HID device not found\n");
674 usb_handle = usb_open(usb_dev);
675 if (usb_handle == NULL) {
676 ast_log(LOG_ERROR, "Not able to open USB device\n");
679 if (usb_claim_interface(usb_handle, C108_HID_INTERFACE) < 0) {
680 if (usb_detach_kernel_driver_np(usb_handle, C108_HID_INTERFACE) < 0) {
681 ast_log(LOG_ERROR, "Not able to detach the USB device\n");
684 if (usb_claim_interface(usb_handle, C108_HID_INTERFACE) < 0) {
685 ast_log(LOG_ERROR, "Not able to claim the USB device\n");
689 memset(buf, 0, sizeof(buf));
690 buf[2] = o->hid_gpio_ctl;
692 hid_set_outputs(usb_handle, buf);
693 traceusb1("hidthread: Starting normally!!\n");
695 while (!o->stophid) {
696 buf[o->hid_gpio_ctl_loc] = o->hid_gpio_ctl;
697 hid_get_inputs(usb_handle, buf);
698 keyed = !(buf[o->hid_io_cor_loc] & o->hid_io_cor);
699 if (keyed != o->rxhidsq) {
701 ast_log(LOG_NOTICE, "chan_usbradio() hidthread: update rxhidsq = %d\n", keyed);
705 /* if change in tx stuff */
707 if (o->txkeyed || o->txchankey || o->txtestkey || o->pmrChan->txPttOut)
710 if (o->lasttx != txtmp) {
713 ast_log(LOG_NOTICE, "hidthread: tx set to %d\n", txtmp);
714 buf[o->hid_gpio_loc] = 0;
716 buf[o->hid_gpio_loc] = o->hid_io_ptt;
717 buf[o->hid_gpio_ctl_loc] = o->hid_gpio_ctl;
718 hid_set_outputs(usb_handle, buf);
721 time(&o->lasthidtime);
724 buf[o->hid_gpio_loc] = 0;
726 buf[o->hid_gpio_loc] = o->hid_io_ptt;
727 buf[o->hid_gpio_ctl_loc] = o->hid_gpio_ctl;
728 hid_set_outputs(usb_handle, buf);
733 * returns a pointer to the descriptor with the given name
735 static struct chan_usbradio_pvt *find_desc(char *dev)
737 struct chan_usbradio_pvt *o = NULL;
740 ast_log(LOG_WARNING, "null dev\n");
742 for (o = usbradio_default.next; o && o->name && dev && strcmp(o->name, dev) != 0; o = o->next);
745 ast_log(LOG_WARNING, "could not find <%s>\n", dev ? dev : "--no-device--");
751 * split a string in extension-context, returns pointers to malloc'ed
753 * If we do not have 'overridecontext' then the last @ is considered as
754 * a context separator, and the context is overridden.
755 * This is usually not very necessary as you can play with the dialplan,
756 * and it is nice not to need it because you have '@' in SIP addresses.
757 * Return value is the buffer address.
760 static char *ast_ext_ctx(const char *src, char **ext, char **ctx)
762 struct chan_usbradio_pvt *o = find_desc(usbradio_active);
764 if (ext == NULL || ctx == NULL)
765 return NULL; /* error */
769 if (src && *src != '\0')
770 *ext = ast_strdup(src);
775 if (!o->overridecontext) {
776 /* parse from the right */
777 *ctx = strrchr(*ext, '@');
787 * Returns the number of blocks used in the audio output channel
789 static int used_blocks(struct chan_usbradio_pvt *o)
791 struct audio_buf_info info;
793 if (ioctl(o->sounddev, SNDCTL_DSP_GETOSPACE, &info)) {
794 if (!(o->warned & WARN_used_blocks)) {
795 ast_log(LOG_WARNING, "Error reading output space\n");
796 o->warned |= WARN_used_blocks;
801 if (o->total_blocks == 0) {
802 ast_debug(4, "fragtotal %d size %d avail %d\n", info.fragstotal, info.fragsize, info.fragments);
803 o->total_blocks = info.fragments;
806 return o->total_blocks - info.fragments;
809 /*! \brief Write an exactly FRAME_SIZE sized frame */
810 static int soundcard_writeframe(struct chan_usbradio_pvt *o, short *data)
815 setformat(o, O_RDWR);
817 return 0; /* not fatal */
819 * Nothing complex to manage the audio device queue.
820 * If the buffer is full just drop the extra, otherwise write.
821 * XXX in some cases it might be useful to write anyways after
822 * a number of failures, to restart the output chain.
824 res = used_blocks(o);
825 if (res > o->queuesize) { /* no room to write a block */
826 if (o->w_errors++ == 0 && (usbradio_debug & 0x4))
827 ast_log(LOG_WARNING, "write: used %d blocks (%d)\n", res, o->w_errors);
832 return write(o->sounddev, ((void *) data), FRAME_SIZE * 2 * 12);
836 * reset and close the device if opened,
837 * then open and initialize it in the desired mode,
838 * trigger reads and writes so we can start using it.
840 static int setformat(struct chan_usbradio_pvt *o, int mode)
842 int fmt, desired, res, fd;
845 if (o->sounddev >= 0) {
846 ioctl(o->sounddev, SNDCTL_DSP_RESET, 0);
851 if (mode == O_CLOSE) /* we are done */
853 if (ast_tvdiff_ms(ast_tvnow(), o->lastopen) < 1000)
854 return -1; /* don't open too often */
855 o->lastopen = ast_tvnow();
856 strcpy(device, "/dev/dsp");
858 snprintf(device + strlen("/dev/dsp"), sizeof(device) - strlen("/dev/dsp"), "%d", o->devicenum);
859 fd = o->sounddev = open(device, mode | O_NONBLOCK);
861 ast_log(LOG_WARNING, "Unable to re-open DSP device %d: %s\n", o->devicenum, strerror(errno));
865 o->owner->fds[0] = fd;
867 #if __BYTE_ORDER == __LITTLE_ENDIAN
872 res = ioctl(fd, SNDCTL_DSP_SETFMT, &fmt);
874 ast_log(LOG_WARNING, "Unable to set format to 16-bit signed\n");
879 res = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
880 /* Check to see if duplex set (FreeBSD Bug) */
881 res = ioctl(fd, SNDCTL_DSP_GETCAPS, &fmt);
882 if (res == 0 && (fmt & DSP_CAP_DUPLEX)) {
883 ast_verb(2, "Console is full duplex\n");
896 res = ioctl(fd, SNDCTL_DSP_STEREO, &fmt);
898 ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
901 fmt = desired = 48000; /* 8000 Hz desired */
902 res = ioctl(fd, SNDCTL_DSP_SPEED, &fmt);
905 ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
908 if (fmt != desired) {
909 if (!(o->warned & WARN_speed)) {
911 "Requested %d Hz, got %d Hz -- sound may be choppy\n",
913 o->warned |= WARN_speed;
917 * on Freebsd, SETFRAGMENT does not work very well on some cards.
918 * Default to use 256 bytes, let the user override
922 res = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &fmt);
924 if (!(o->warned & WARN_frag)) {
926 "Unable to set fragment size -- sound may be choppy\n");
927 o->warned |= WARN_frag;
931 /* on some cards, we need SNDCTL_DSP_SETTRIGGER to start outputting */
932 res = PCM_ENABLE_INPUT | PCM_ENABLE_OUTPUT;
933 res = ioctl(fd, SNDCTL_DSP_SETTRIGGER, &res);
934 /* it may fail if we are in half duplex, never mind */
939 * some of the standard methods supported by channels.
941 static int usbradio_digit_begin(struct ast_channel *c, char digit)
946 static int usbradio_digit_end(struct ast_channel *c, char digit, unsigned int duration)
948 /* no better use for received digits than print them */
949 ast_verb(0, " << Console Received digit %c of duration %u ms >> \n",
954 static int usbradio_text(struct ast_channel *c, const char *text)
956 /* print received messages */
957 ast_verb(0, " << Console Received text %s >> \n", text);
962 * handler for incoming calls. Either autoanswer, or start ringing
964 static int usbradio_call(struct ast_channel *c, char *dest, int timeout)
966 struct chan_usbradio_pvt *o = c->tech_pvt;
968 time(&o->lasthidtime);
969 ast_pthread_create_background(&o->hidthread, NULL, hidthread, o);
970 ast_setstate(c, AST_STATE_UP);
975 * remote side answered the phone
977 static int usbradio_answer(struct ast_channel *c)
979 ast_setstate(c, AST_STATE_UP);
984 static int usbradio_hangup(struct ast_channel *c)
986 struct chan_usbradio_pvt *o = c->tech_pvt;
990 ast_module_unref(ast_module_info->self);
992 if (o->autoanswer || o->autohangup) {
993 /* Assume auto-hangup too */
995 setformat(o, O_CLOSE);
999 pthread_join(o->hidthread, NULL);
1004 /* used for data coming from the network */
1005 static int usbradio_write(struct ast_channel *c, struct ast_frame *f)
1008 struct chan_usbradio_pvt *o = c->tech_pvt;
1010 traceusb2("usbradio_write() o->nosound=%d\n", o->nosound); /*sph maw asdf */
1013 * we could receive a block which is not a multiple of our
1014 * FRAME_SIZE, so buffer it locally and write to the device
1015 * in FRAME_SIZE chunks.
1016 * Keep the residue stored for future use.
1019 if (o->txkeyed || o->txtestkey)
1020 o->pmrChan->txPttIn = 1;
1022 o->pmrChan->txPttIn = 0;
1024 #if DEBUG_CAPTURES == 1 /* to write input data to a file datalen=320 */
1025 if (ftxcapraw && o->b.txcapraw) {
1026 i16 i, tbuff[f->datalen];
1027 for (i = 0; i < f->datalen; i += 2) {
1028 tbuff[i] = ((i16 *)(f->data))[i / 2];
1029 tbuff[i + 1] = o->txkeyed * M_Q13;
1031 fwrite(tbuff, 2, f->datalen, ftxcapraw);
1032 /*fwrite(f->data,1,f->datalen,ftxcapraw); */
1036 PmrTx(o->pmrChan,(i16*)f->data,(i16*)o->usbradio_write_buf_1);
1038 #if 0 /* to write 48KS/s stereo data to a file */
1039 if (!ftxoutraw) ftxoutraw = fopen(TX_CAP_OUT_FILE,"w");
1040 if (ftxoutraw) fwrite(o->usbradio_write_buf_1,1,f->datalen * 2 * 6,ftxoutraw);
1043 #if DEBUG_CAPTURES == 1
1044 if (o->b.txcap2 && ftxcaptrace)
1045 fwrite((o->pmrChan->ptxDebug), 1, FRAME_SIZE * 2 * 16, ftxcaptrace);
1048 src = 0; /* read position into f->data */
1049 datalen = f->datalen * 12;
1050 while (src < datalen) {
1051 /* Compute spare room in the buffer */
1052 int l = sizeof(o->usbradio_write_buf) - o->usbradio_write_dst;
1054 if (datalen - src >= l) { /* enough to fill a frame */
1055 memcpy(o->usbradio_write_buf + o->usbradio_write_dst, o->usbradio_write_buf_1 + src, l);
1056 soundcard_writeframe(o, (short *) o->usbradio_write_buf);
1058 o->usbradio_write_dst = 0;
1059 } else { /* copy residue */
1061 memcpy(o->usbradio_write_buf + o->usbradio_write_dst, o->usbradio_write_buf_1 + src, l);
1062 src += l; /* but really, we are done */
1063 o->usbradio_write_dst += l;
1069 static struct ast_frame *usbradio_read(struct ast_channel *c)
1072 struct chan_usbradio_pvt *o = c->tech_pvt;
1073 struct ast_frame *f = &o->read_f, *f1;
1074 struct ast_frame wf = { AST_FRAME_CONTROL };
1077 traceusb2("usbradio_read()\n"); /* sph maw asdf */
1079 if (o->lasthidtime) {
1081 if ((now - o->lasthidtime) > 3) {
1082 ast_log(LOG_ERROR, "HID process has died or something!!\n");
1086 if (o->lastrx && (!o->rxkeyed)) {
1088 wf.subclass = AST_CONTROL_RADIO_UNKEY;
1089 ast_queue_frame(o->owner, &wf);
1090 } else if ((!o->lastrx) && (o->rxkeyed)) {
1092 wf.subclass = AST_CONTROL_RADIO_KEY;
1093 ast_queue_frame(o->owner, &wf);
1095 /* XXX can be simplified returning &ast_null_frame */
1096 /* prepare a NULL frame in case we don't have enough data to return */
1097 memset(f, 0, sizeof(struct ast_frame));
1098 f->frametype = AST_FRAME_NULL;
1099 f->src = usbradio_tech.type;
1101 res = read(o->sounddev, o->usbradio_read_buf + o->readpos,
1102 sizeof(o->usbradio_read_buf) - o->readpos);
1103 if (res < 0) /* audio data not ready, return a NULL frame */
1107 if (o->readpos < sizeof(o->usbradio_read_buf)) /* not enough samples */
1113 #if DEBUG_CAPTURES == 1
1114 if (o->b.rxcapraw && frxcapraw)
1115 fwrite((o->usbradio_read_buf + AST_FRIENDLY_OFFSET), 1, FRAME_SIZE * 2 * 2 * 6, frxcapraw);
1120 (i16 *)(o->usbradio_read_buf + AST_FRIENDLY_OFFSET),
1121 (i16 *)(o->usbradio_read_buf_8k + AST_FRIENDLY_OFFSET));
1124 static FILE *hInput;
1125 i16 iBuff[FRAME_SIZE * 2 * 6];
1127 o->pmrChan->b.rxCapture = 1;
1130 hInput = fopen("/usr/src/xpmr/testdata/rx_in.pcm", "r");
1132 ast_log(LOG_ERROR, " Input Data File Not Found.\n");
1137 if (0 == fread((void *)iBuff, 2, FRAME_SIZE * 2 * 6, hInput))
1142 (i16 *)(o->usbradio_read_buf_8k + AST_FRIENDLY_OFFSET));
1147 if (!frxoutraw) frxoutraw = fopen(RX_CAP_OUT_FILE, "w");
1148 if (frxoutraw) fwrite((o->usbradio_read_buf_8k + AST_FRIENDLY_OFFSET), 1, FRAME_SIZE * 2, frxoutraw);
1151 #if DEBUG_CAPTURES == 1
1152 if (frxcaptrace && o->b.rxcap2) fwrite((o->pmrChan->prxDebug), 1, FRAME_SIZE * 2 * 16, frxcaptrace);
1155 if (o->rxcdtype == CD_HID && (o->pmrChan->rxExtCarrierDetect != o->rxhidsq))
1156 o->pmrChan->rxExtCarrierDetect = o->rxhidsq;
1157 if (o->rxcdtype == CD_HID_INVERT && (o->pmrChan->rxExtCarrierDetect == o->rxhidsq))
1158 o->pmrChan->rxExtCarrierDetect = !o->rxhidsq;
1160 if ( (o->rxcdtype == CD_HID && o->rxhidsq) ||
1161 (o->rxcdtype == CD_HID_INVERT && !o->rxhidsq) ||
1162 (o->rxcdtype == CD_XPMR_NOISE && o->pmrChan->rxCarrierDetect) ||
1163 (o->rxcdtype == CD_XPMR_VOX && o->pmrChan->rxCarrierDetect) )
1168 if (res != o->rxcarrierdetect) {
1169 o->rxcarrierdetect = res;
1171 ast_debug(4, "rxcarrierdetect = %d\n", res);
1174 if (o->pmrChan->rxCtcss->decode != o->rxctcssdecode) {
1176 ast_debug(4, "rxctcssdecode = %d\n", o->pmrChan->rxCtcss->decode);
1177 o->rxctcssdecode = o->pmrChan->rxCtcss->decode;
1180 if ( ( o->rxctcssfreq && (o->rxctcssdecode == o->pmrChan->rxCtcssIndex)) ||
1181 ( !o->rxctcssfreq && o->rxcarrierdetect) )
1187 o->readpos = AST_FRIENDLY_OFFSET; /* reset read pointer for next frame */
1188 if (c->_state != AST_STATE_UP) /* drop data if frame is not up */
1190 /* ok we can build and deliver the frame to the caller */
1191 f->frametype = AST_FRAME_VOICE;
1192 f->subclass = AST_FORMAT_SLINEAR;
1193 f->samples = FRAME_SIZE;
1194 f->datalen = FRAME_SIZE * 2;
1195 f->data = o->usbradio_read_buf_8k + AST_FRIENDLY_OFFSET;
1196 if (o->boost != BOOST_SCALE) { /* scale and clip values */
1198 int16_t *p = (int16_t *) f->data;
1199 for (i = 0; i < f->samples; i++) {
1200 x = (p[i] * o->boost) / BOOST_SCALE;
1203 else if (x < -32768)
1209 f->offset = AST_FRIENDLY_OFFSET;
1211 f1 = ast_dsp_process(c, o->dsp, f);
1212 if ((f1->frametype == AST_FRAME_DTMF_END) || (f1->frametype == AST_FRAME_DTMF_BEGIN)) {
1213 if (f1->frametype == AST_FRAME_DTMF_END)
1214 ast_log(LOG_NOTICE,"Got DTMF char %c\n",f1->subclass);
1221 static int usbradio_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
1223 struct chan_usbradio_pvt *o = newchan->tech_pvt;
1224 ast_log(LOG_WARNING,"usbradio_fixup()\n");
1229 static int usbradio_indicate(struct ast_channel *c, int cond, const void *data, size_t datalen)
1231 struct chan_usbradio_pvt *o = c->tech_pvt;
1235 case AST_CONTROL_BUSY:
1236 case AST_CONTROL_CONGESTION:
1237 case AST_CONTROL_RINGING:
1241 case AST_CONTROL_PROGRESS:
1242 case AST_CONTROL_PROCEEDING:
1243 case AST_CONTROL_VIDUPDATE:
1245 case AST_CONTROL_HOLD:
1246 ast_verb(0, " << Console Has Been Placed on Hold >> \n");
1247 ast_moh_start(c, data, o->mohinterpret);
1249 case AST_CONTROL_UNHOLD:
1250 ast_verb(0, " << Console Has Been Retrieved from Hold >> \n");
1253 case AST_CONTROL_RADIO_KEY:
1256 ast_verb(0, " << Radio Transmit On. >> \n");
1258 case AST_CONTROL_RADIO_UNKEY:
1261 ast_verb(0, " << Radio Transmit Off. >> \n");
1264 ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, c->name);
1272 * allocate a new channel.
1274 static struct ast_channel *usbradio_new(struct chan_usbradio_pvt *o, char *ext, char *ctx, int state)
1276 struct ast_channel *c;
1277 char device[15] = "dsp";
1280 snprintf(device + 3, sizeof(device) - 3, "%d", o->devicenum);
1281 c = ast_channel_alloc(1, state, o->cid_num, o->cid_name, "", ext, ctx, 0, "usbRadio/%s", device);
1284 c->tech = &usbradio_tech;
1285 if (o->sounddev < 0)
1286 setformat(o, O_RDWR);
1287 c->fds[0] = o->sounddev; /* -1 if device closed, override later */
1288 c->nativeformats = AST_FORMAT_SLINEAR;
1289 c->readformat = AST_FORMAT_SLINEAR;
1290 c->writeformat = AST_FORMAT_SLINEAR;
1293 if (!ast_strlen_zero(o->language))
1294 ast_string_field_set(c, language, o->language);
1295 /* Don't use ast_set_callerid() here because it will
1296 * generate a needless NewCallerID event */
1297 c->cid.cid_num = ast_strdup(o->cid_num);
1298 c->cid.cid_ani = ast_strdup(o->cid_num);
1299 c->cid.cid_name = ast_strdup(o->cid_name);
1300 if (!ast_strlen_zero(ext))
1301 c->cid.cid_dnid = ast_strdup(ext);
1304 ast_module_ref(ast_module_info->self);
1305 ast_jb_configure(c, &global_jbconf);
1306 if (state != AST_STATE_DOWN) {
1307 if (ast_pbx_start(c)) {
1308 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", c->name);
1310 o->owner = c = NULL;
1311 /* XXX what about the channel itself ? */
1312 /* XXX what about usecnt ? */
1319 static struct ast_channel *usbradio_request(const char *type, int format, void *data, int *cause)
1321 struct ast_channel *c;
1322 struct chan_usbradio_pvt *o = find_desc(data);
1324 ast_debug(4, "usbradio_request ty <%s> data 0x%p <%s>\n", type, data, (char *) data);
1326 ast_log(LOG_NOTICE, "Device %s not found\n", (char *) data);
1327 /* XXX we could default to 'dsp' perhaps ? */
1330 if ((format & AST_FORMAT_SLINEAR) == 0) {
1331 ast_log(LOG_NOTICE, "Format 0x%x unsupported\n", format);
1335 ast_log(LOG_NOTICE, "Already have a call (chan %p) on the usb channel\n", o->owner);
1336 *cause = AST_CAUSE_BUSY;
1339 c = usbradio_new(o, NULL, NULL, AST_STATE_DOWN);
1341 ast_log(LOG_WARNING, "Unable to create new usb channel\n");
1347 static char *handle_cli_radio_key(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1349 struct chan_usbradio_pvt *o = NULL;
1353 e->command = "radio key";
1355 "Usage: radio key\n"
1356 " Simulates COR active.\n";
1363 return CLI_SHOWUSAGE;
1365 o = find_desc(usbradio_active);
1371 static char *handle_cli_radio_unkey(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1373 struct chan_usbradio_pvt *o = NULL;
1377 e->command = "radio unkey";
1379 "Usage: radio unkey\n"
1380 " Simulates COR un-active.\n";
1387 return CLI_SHOWUSAGE;
1389 o = find_desc(usbradio_active);
1395 static char *handle_cli_radio_tune(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1397 struct chan_usbradio_pvt *o = NULL;
1402 e->command = "radio tune [rxnoise|rxvoice|rxtone|rxsquelch|rxcap|rxtracecap|"
1403 "txvoice|txtone|txcap|txtracecap|auxvoice|nocap|dump|save]";
1404 /* radio tune 6 3000 measured tx value */
1406 "Usage: radio tune <function>\n"
1410 " rxsquelch [newsetting]\n"
1413 " txvoice [newsetting]\n"
1414 " txtone [newsetting]\n"
1417 " auxvoice [newsetting]\n"
1420 " save (settings to tuning file)\n"
1422 " All [newsetting]s are values 0-999\n";
1428 if ((a->argc < 2) || (a->argc > 4))
1429 return CLI_SHOWUSAGE;
1431 if (a->argc == 2) { /* just show stuff */
1432 ast_cli(a->fd, "Output A is currently set to %s.\n",
1433 o->txmixa == TX_OUT_COMPOSITE ? "composite" :
1434 o->txmixa == TX_OUT_VOICE ? "voice" :
1435 o->txmixa == TX_OUT_LSD ? "tone" :
1436 o->txmixa == TX_OUT_AUX ? "auxvoice" :
1439 ast_cli(a->fd, "Output B is currently set to %s.\n",
1440 o->txmixb == TX_OUT_COMPOSITE ? "composite" :
1441 o->txmixb == TX_OUT_VOICE ? "voice" :
1442 o->txmixb == TX_OUT_LSD ? "tone" :
1443 o->txmixb == TX_OUT_AUX ? "auxvoice" :
1446 ast_cli(a->fd, "Tx Voice Level currently set to %d\n", o->txmixaset);
1447 ast_cli(a->fd, "Tx Tone Level currently set to %d\n", o->txctcssadj);
1448 ast_cli(a->fd, "Rx Squelch currently set to %d\n", o->rxsquelchadj);
1449 return CLI_SHOWUSAGE;
1452 o = find_desc(usbradio_active);
1454 if (!strcasecmp(a->argv[2], "rxnoise"))
1456 else if (!strcasecmp(a->argv[2], "rxvoice"))
1458 else if (!strcasecmp(a->argv[2], "rxtone"))
1460 else if (!strcasecmp(a->argv[2], "rxsquelch")) {
1462 ast_cli(a->fd, "Current Signal Strength is %d\n", ((32767 - o->pmrChan->rxRssi) * 1000 / 32767));
1463 ast_cli(a->fd, "Current Squelch setting is %d\n", o->rxsquelchadj);
1465 ast_cli(a->fd,"Current Raw RSSI is %d\n",o->pmrChan->rxRssi);
1466 ast_cli(a->fd,"Current (real) Squelch setting is %d\n",*(o->pmrChan->prxSquelchAdjust));
1469 i = atoi(a->argv[3]);
1470 if ((i < 0) || (i > 999))
1471 return CLI_SHOWUSAGE;
1472 ast_cli(a->fd, "Changed Squelch setting to %d\n", i);
1473 o->rxsquelchadj = i;
1474 *(o->pmrChan->prxSquelchAdjust) = ((999 - i) * 32767) / 1000;
1476 } else if (!strcasecmp(a->argv[2], "txvoice")) {
1479 if ((o->txmixa != TX_OUT_VOICE) && (o->txmixb != TX_OUT_VOICE) &&
1480 (o->txmixa != TX_OUT_COMPOSITE) && (o->txmixb != TX_OUT_COMPOSITE)) {
1481 ast_log(LOG_ERROR, "No txvoice output configured.\n");
1482 } else if (a->argc == 3) {
1483 if ((o->txmixa == TX_OUT_VOICE) || (o->txmixa == TX_OUT_COMPOSITE))
1484 ast_cli(a->fd, "Current txvoice setting on Channel A is %d\n", o->txmixaset);
1486 ast_cli(a->fd, "Current txvoice setting on Channel B is %d\n", o->txmixbset);
1488 i = atoi(a->argv[3]);
1489 if ((i < 0) || (i > 999))
1490 return CLI_SHOWUSAGE;
1492 if ((o->txmixa == TX_OUT_VOICE) || (o->txmixa == TX_OUT_COMPOSITE)) {
1494 ast_cli(a->fd, "Changed txvoice setting on Channel A to %d\n", o->txmixaset);
1497 ast_cli(a->fd, "Changed txvoice setting on Channel B to %d\n", o->txmixbset);
1501 ast_cli(a->fd, "Changed Tx Voice Output setting to %d\n", i);
1504 } else if (!strcasecmp(a->argv[2], "auxvoice")) {
1506 if ( (o->txmixa != TX_OUT_AUX) && (o->txmixb != TX_OUT_AUX))
1507 ast_log(LOG_WARNING, "No auxvoice output configured.\n");
1508 else if (a->argc == 3) {
1509 if (o->txmixa == TX_OUT_AUX)
1510 ast_cli(a->fd, "Current auxvoice setting on Channel A is %d\n", o->txmixaset);
1512 ast_cli(a->fd, "Current auxvoice setting on Channel B is %d\n", o->txmixbset);
1514 i = atoi(a->argv[3]);
1515 if ((i < 0) || (i > 999))
1516 return CLI_SHOWUSAGE;
1517 if (o->txmixa == TX_OUT_AUX) {
1519 ast_cli(a->fd, "Changed auxvoice setting on Channel A to %d\n", o->txmixaset);
1522 ast_cli(a->fd, "Changed auxvoice setting on Channel B to %d\n", o->txmixbset);
1527 /* tune_auxoutput(o,i); */
1528 } else if (!strcasecmp(a->argv[2], "txtone")) {
1530 ast_cli(a->fd, "Current Tx CTCSS modulation setting = %d\n", o->txctcssadj);
1532 i = atoi(a->argv[3]);
1533 if ((i < 0) || (i > 999))
1534 return CLI_SHOWUSAGE;
1536 set_txctcss_level(o);
1537 ast_cli(a->fd, "Changed Tx CTCSS modulation setting to %i\n", i);
1542 } else if (!strcasecmp(a->argv[2],"dump"))
1544 else if (!strcasecmp(a->argv[2],"nocap")) {
1545 ast_cli(a->fd, "File capture (trace) was rx=%d tx=%d and now off.\n", o->b.rxcap2, o->b.txcap2);
1546 ast_cli(a->fd, "File capture (raw) was rx=%d tx=%d and now off.\n", o->b.rxcapraw, o->b.txcapraw);
1547 o->b.rxcapraw = o->b.txcapraw = o->b.rxcap2 = o->b.txcap2 = o->pmrChan->b.rxCapture = o->pmrChan->b.txCapture = 0;
1553 fclose(frxcaptrace);
1565 fclose(ftxcaptrace);
1572 } else if (!strcasecmp(a->argv[2], "rxtracecap")) {
1574 frxcaptrace = fopen(RX_CAP_TRACE_FILE, "w");
1575 ast_cli(a->fd, "Trace rx on.\n");
1576 o->b.rxcap2 = o->pmrChan->b.rxCapture = 1;
1577 } else if (!strcasecmp(a->argv[2], "txtracecap")) {
1579 ftxcaptrace = fopen(TX_CAP_TRACE_FILE, "w");
1580 ast_cli(a->fd, "Trace tx on.\n");
1581 o->b.txcap2 = o->pmrChan->b.txCapture = 1;
1582 } else if (!strcasecmp(a->argv[2], "rxcap")) {
1584 frxcapraw = fopen(RX_CAP_RAW_FILE, "w");
1585 ast_cli(a->fd, "cap rx raw on.\n");
1587 } else if (!strcasecmp(a->argv[2], "txcap")) {
1589 ftxcapraw = fopen(TX_CAP_RAW_FILE, "w");
1590 ast_cli(a->fd, "cap tx raw on.\n");
1592 } else if (!strcasecmp(a->argv[2], "save")) {
1594 ast_cli(a->fd, "Saved radio tuning settings to usbradio_tune.conf\n");
1596 return CLI_SHOWUSAGE;
1601 set transmit ctcss modulation level
1602 adjust mixer output or internal gain depending on output type
1603 setting range is 0.0 to 0.9
1605 static int set_txctcss_level(struct chan_usbradio_pvt *o)
1607 if (o->txmixa == TX_OUT_LSD) {
1608 o->txmixaset = (151 * o->txctcssadj) / 1000;
1611 } else if (o->txmixb == TX_OUT_LSD) {
1612 o->txmixbset = (151 * o->txctcssadj) / 1000;
1616 *o->pmrChan->ptxCtcssAdjust = (o->txctcssadj * M_Q8) / 1000;
1621 CLI debugging on and off
1623 static char *handle_cli_radio_set_debug_deprecated(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1625 struct chan_usbradio_pvt *o = NULL;
1629 e->command = "radio set debug [off]";
1631 "Usage: radio set debug [off]\n"
1632 " Enable/Disable radio debugging.\n";
1636 if (a->argc < 3 || a->argc > 4)
1637 return CLI_SHOWUSAGE;
1638 if (a->argc == 4 && strncasecmp(a->argv[3], "off", 3))
1639 return CLI_SHOWUSAGE;
1641 o = find_desc(usbradio_active);
1648 ast_cli(a->fd, "USB Radio debugging %s.\n", o->debuglevel ? "enabled" : "disabled");
1653 static char *handle_cli_radio_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1655 struct chan_usbradio_pvt *o = NULL;
1659 e->command = "radio set debug {on|off}";
1661 "Usage: radio set debug {on|off}\n"
1662 " Enable/Disable radio debugging.\n";
1667 if (a->argc != e->args)
1668 return CLI_SHOWUSAGE;
1670 o = find_desc(usbradio_active);
1672 if (!strncasecmp(a->argv[e->args - 1], "on", 2))
1674 else if (!strncasecmp(a->argv[e->args - 1], "off", 3))
1677 return CLI_SHOWUSAGE;
1679 ast_cli(a->fd, "USB Radio debugging %s.\n", o->debuglevel ? "enabled" : "disabled");
1684 static struct ast_cli_entry cli_radio_set_debug_deprecated = AST_CLI_DEFINE(handle_cli_radio_set_debug_deprecated, "Enable/Disable Radio Debugging");
1685 static struct ast_cli_entry cli_usbradio[] = {
1686 AST_CLI_DEFINE(handle_cli_radio_key, "Simulate Rx Signal Present"),
1687 AST_CLI_DEFINE(handle_cli_radio_unkey, "Simulate Rx Signal Lusb"),
1688 AST_CLI_DEFINE(handle_cli_radio_tune, "Radio Tune"),
1689 AST_CLI_DEFINE(handle_cli_radio_set_debug, "Enable/Disable Radio Debugging", .deprecate_cmd = &cli_radio_set_debug_deprecated),
1693 * store the callerid components
1696 static void store_callerid(struct chan_usbradio_pvt *o, const char *s)
1698 ast_callerid_split(s, o->cid_name, sizeof(o->cid_name), o->cid_num, sizeof(o->cid_num));
1702 static void store_rxdemod(struct chan_usbradio_pvt *o, const char *s)
1704 if (!strcasecmp(s, "no")) {
1705 o->rxdemod = RX_AUDIO_NONE;
1706 } else if (!strcasecmp(s, "speaker")) {
1707 o->rxdemod = RX_AUDIO_SPEAKER;
1708 } else if (!strcasecmp(s, "flat")) {
1709 o->rxdemod = RX_AUDIO_FLAT;
1711 ast_log(LOG_WARNING, "Unrecognized rxdemod parameter: %s\n", s);
1714 ast_debug(4, "set rxdemod = %s\n", s);
1718 static void store_txmixa(struct chan_usbradio_pvt *o, const char *s)
1720 if (!strcasecmp(s, "no"))
1721 o->txmixa = TX_OUT_OFF;
1723 else if (!strcasecmp(s, "voice"))
1724 o->txmixa = TX_OUT_VOICE;
1725 else if (!strcasecmp(s, "tone"))
1726 o->txmixa = TX_OUT_LSD;
1727 else if (!strcasecmp(s, "composite"))
1728 o->txmixa = TX_OUT_COMPOSITE;
1729 else if (!strcasecmp(s, "auxvoice"))
1730 o->txmixb = TX_OUT_AUX;
1732 ast_log(LOG_WARNING, "Unrecognized txmixa parameter: %s\n", s);
1734 ast_debug(4, "set txmixa = %s\n", s);
1737 static void store_txmixb(struct chan_usbradio_pvt *o, const char *s)
1739 if (!strcasecmp(s, "no"))
1740 o->txmixb = TX_OUT_OFF;
1741 else if (!strcasecmp(s, "voice"))
1742 o->txmixb = TX_OUT_VOICE;
1743 else if (!strcasecmp(s, "tone"))
1744 o->txmixb = TX_OUT_LSD;
1745 else if (!strcasecmp(s, "composite"))
1746 o->txmixb = TX_OUT_COMPOSITE;
1747 else if (!strcasecmp(s, "auxvoice"))
1748 o->txmixb = TX_OUT_AUX;
1750 ast_log(LOG_WARNING, "Unrecognized txmixb parameter: %s\n", s);
1752 ast_debug(4, "set txmixb = %s\n", s);
1755 static void store_rxcdtype(struct chan_usbradio_pvt *o, const char *s)
1757 if (!strcasecmp(s, "no"))
1758 o->rxcdtype = CD_IGNORE;
1759 else if (!strcasecmp(s, "usb"))
1760 o->rxcdtype = CD_HID;
1761 else if (!strcasecmp(s, "dsp"))
1762 o->rxcdtype = CD_XPMR_NOISE;
1763 else if (!strcasecmp(s, "vox"))
1764 o->rxcdtype = CD_XPMR_VOX;
1765 else if (!strcasecmp(s, "usbinvert"))
1766 o->rxcdtype = CD_HID_INVERT;
1768 ast_log(LOG_WARNING, "Unrecognized rxcdtype parameter: %s\n", s);
1770 ast_debug(4, "set rxcdtype = %s\n", s);
1773 static void store_rxsdtype(struct chan_usbradio_pvt *o, const char *s)
1775 if (!strcasecmp(s, "no") || !strcasecmp(s, "SD_IGNORE"))
1776 o->rxsdtype = SD_IGNORE;
1777 else if (!strcasecmp(s, "usb") || !strcasecmp(s, "SD_HID"))
1778 o->rxsdtype = SD_HID;
1779 else if (!strcasecmp(s, "usbinvert") || !strcasecmp(s, "SD_HID_INVERT"))
1780 o->rxsdtype = SD_HID_INVERT;
1781 else if (!strcasecmp(s, "software") || !strcasecmp(s, "SD_XPMR"))
1782 o->rxsdtype = SD_XPMR;
1784 ast_log(LOG_WARNING, "Unrecognized rxsdtype parameter: %s\n", s);
1786 ast_debug(4, "set rxsdtype = %s\n", s);
1789 static void store_rxgain(struct chan_usbradio_pvt *o, const char *s)
1792 if (sscanf(s, "%f", &f) == 1)
1794 ast_debug(4, "set rxgain = %f\n", f);
1797 static void store_rxvoiceadj(struct chan_usbradio_pvt *o, const char *s)
1800 if (sscanf(s, "%f", &f) == 1)
1802 ast_debug(4, "set rxvoiceadj = %f\n", f);
1805 static void store_rxctcssadj(struct chan_usbradio_pvt *o, const char *s)
1808 if (sscanf(s, "%f", &f) == 1)
1810 ast_debug(4, "set rxctcssadj = %f\n", f);
1813 static void store_txtoctype(struct chan_usbradio_pvt *o, const char *s)
1815 if (!strcasecmp(s, "no") || !strcasecmp(s, "TOC_NONE"))
1816 o->txtoctype = TOC_NONE;
1817 else if (!strcasecmp(s, "phase") || !strcasecmp(s, "TOC_PHASE"))
1818 o->txtoctype = TOC_PHASE;
1819 else if (!strcasecmp(s, "notone") || !strcasecmp(s, "TOC_NOTONE"))
1820 o->txtoctype = TOC_NOTONE;
1822 ast_log(LOG_WARNING, "Unrecognized txtoctype parameter: %s\n", s);
1824 ast_debug(4, "set txtoctype = %s\n", s);
1827 static void store_rxctcssfreq(struct chan_usbradio_pvt *o, const char *s)
1830 if (sscanf(s, "%f", &f) == 1)
1832 ast_debug(4, "set rxctcss = %f\n", f);
1835 static void store_txctcssfreq(struct chan_usbradio_pvt *o, const char *s)
1838 if (sscanf(s, "%f", &f) == 1)
1840 ast_debug(4, "set txctcss = %f\n", f);
1843 static void tune_txoutput(struct chan_usbradio_pvt *o, int value)
1846 o->pmrChan->txPttIn = 1;
1849 /* generate 1KHz tone at 7200 peak */
1850 o->pmrChan->spsSigGen1->freq = 10000;
1851 o->pmrChan->spsSigGen1->outputGain = (float)(0.22 * M_Q8);
1852 o->pmrChan->b.startSpecialTone = 1;
1855 TxTestTone(o->pmrChan, 1);
1858 /* o->pmrChan->b.stopSpecialTone = 1; */
1861 TxTestTone(o->pmrChan, 0);
1863 o->pmrChan->txPttIn = 0;
1867 static void tune_rxinput(struct chan_usbradio_pvt *o)
1869 const int target = 23000;
1870 const int tolerance = 2000;
1871 const int settingmin = 1;
1872 const int settingstart = 2;
1873 const int maxtries = 12;
1877 int setting = 0, tries = 0, tmpdiscfactor, meas;
1880 settingmax = o->micmax;
1882 if (o->pmrChan->rxDemod)
1885 setting = settingstart;
1887 while (tries < maxtries) {
1888 setamixer(o->devicenum, MIXER_PARAM_MIC_CAPTURE_VOL, setting, 0);
1889 setamixer(o->devicenum, MIXER_PARAM_MIC_BOOST, o->rxboostset, 0);
1891 if (o->rxcdtype == CD_XPMR_VOX || o->rxdemod == RX_AUDIO_SPEAKER) {
1892 ast_debug(4, "Measure Direct Input\n");
1893 o->pmrChan->spsMeasure->source = o->pmrChan->spsRx->source;
1894 o->pmrChan->spsMeasure->discfactor = 1000;
1895 o->pmrChan->spsMeasure->enabled = 1;
1896 o->pmrChan->spsMeasure->amax = o->pmrChan->spsMeasure->amin = 0;
1898 meas = o->pmrChan->spsMeasure->apeak;
1899 o->pmrChan->spsMeasure->enabled = 0;
1901 ast_debug(4, "Measure HF Noise\n");
1902 tmpdiscfactor = o->pmrChan->spsRx->discfactor;
1903 o->pmrChan->spsRx->discfactor = (i16)1000;
1904 o->pmrChan->spsRx->discounteru = o->pmrChan->spsRx->discounterl = 0;
1905 o->pmrChan->spsRx->amax = o->pmrChan->spsRx->amin = 0;
1907 meas = o->pmrChan->rxRssi;
1908 o->pmrChan->spsRx->discfactor = tmpdiscfactor;
1909 o->pmrChan->spsRx->discounteru = o->pmrChan->spsRx->discounterl = 0;
1910 o->pmrChan->spsRx->amax = o->pmrChan->spsRx->amin = 0;
1914 ast_log(LOG_NOTICE, "tries=%d, setting=%d, meas=%i\n", tries, setting, meas);
1916 if ( meas < (target - tolerance) || meas > (target + tolerance) || tries < 3)
1917 setting = setting * target / meas;
1918 else if (tries > 4 && meas > (target - tolerance) && meas < (target + tolerance) )
1921 if (setting < settingmin)
1922 setting = settingmin;
1923 else if (setting > settingmax)
1924 setting = settingmax;
1928 ast_log(LOG_NOTICE, "DONE tries=%d, setting=%d, meas=%i\n", tries,
1929 (setting * 1000) / o->micmax, meas);
1930 if (meas < (target - tolerance) || meas > (target + tolerance))
1931 ast_log(LOG_NOTICE, "ERROR: RX INPUT ADJUST FAILED.\n");
1933 ast_log(LOG_NOTICE, "INFO: RX INPUT ADJUST SUCCESS.\n");
1934 o->rxmixerset = (setting * 1000) / o->micmax;
1939 static void tune_rxvoice(struct chan_usbradio_pvt *o)
1941 const int target = 7200; /* peak */
1942 const int tolerance = 360; /* peak to peak */
1943 const float settingmin = 0.1;
1944 const float settingmax = 4;
1945 const float settingstart = 1;
1946 const int maxtries = 12;
1950 int tries = 0, meas;
1952 ast_log(LOG_NOTICE, "INFO: RX VOICE ADJUST START.\n");
1953 ast_log(LOG_NOTICE, "target=%d tolerance=%d\n", target, tolerance);
1955 if (!o->pmrChan->spsMeasure)
1956 ast_log(LOG_ERROR, "NO MEASURE BLOCK.\n");
1958 if (!o->pmrChan->spsMeasure->source || !o->pmrChan->prxVoiceAdjust )
1959 ast_log(LOG_ERROR, "NO SOURCE OR MEASURE SETTING.\n");
1961 o->pmrChan->spsMeasure->source = o->pmrChan->spsRxOut->sink;
1962 o->pmrChan->spsMeasure->enabled = 1;
1963 o->pmrChan->spsMeasure->discfactor = 1000;
1965 setting=settingstart;
1967 ast_debug(4, "ERROR: NO MEASURE BLOCK.\n");
1969 while (tries < maxtries) {
1970 *(o->pmrChan->prxVoiceAdjust) = setting * M_Q8;
1972 o->pmrChan->spsMeasure->amax = o->pmrChan->spsMeasure->amin = 0;
1974 meas = o->pmrChan->spsMeasure->apeak;
1975 ast_log(LOG_NOTICE, "tries=%d, setting=%f, meas=%i\n", tries, setting, meas);
1977 if (meas < (target - tolerance) || meas > (target + tolerance) || tries < 3)
1978 setting = setting * target / meas;
1979 else if (tries > 4 && meas > (target - tolerance) && meas < (target + tolerance))
1981 if (setting < settingmin)
1982 setting = settingmin;
1983 else if (setting > settingmax)
1984 setting = settingmax;
1989 o->pmrChan->spsMeasure->enabled = 0;
1991 ast_log(LOG_NOTICE, "DONE tries=%d, setting=%f, meas=%f\n", tries, setting, (float)meas);
1992 if (meas < (target - tolerance) || meas > (target + tolerance))
1993 ast_log(LOG_ERROR, "RX VOICE GAIN ADJUST FAILED.\n");
1995 ast_log(LOG_NOTICE, "RX VOICE GAIN ADJUST SUCCESS.\n");
1996 o->rxvoiceadj = setting;
2000 static void tune_rxctcss(struct chan_usbradio_pvt *o)
2002 const int target = 4096;
2003 const int tolerance = 100;
2004 const float settingmin = 0.1;
2005 const float settingmax = 4;
2006 const float settingstart = 1;
2007 const int maxtries = 12;
2010 int tries = 0, meas;
2012 ast_log(LOG_NOTICE, "RX CTCSS ADJUST START.\n");
2013 ast_log(LOG_NOTICE, "target=%d tolerance=%d \n", target, tolerance);
2015 o->pmrChan->spsMeasure->source = o->pmrChan->prxCtcssMeasure;
2016 o->pmrChan->spsMeasure->discfactor = 400;
2017 o->pmrChan->spsMeasure->enabled = 1;
2019 setting = settingstart;
2021 while (tries < maxtries) {
2022 *(o->pmrChan->prxCtcssAdjust) = setting * M_Q8;
2024 o->pmrChan->spsMeasure->amax = o->pmrChan->spsMeasure->amin = 0;
2026 meas = o->pmrChan->spsMeasure->apeak;
2027 ast_debug(4, "tries=%d, setting=%f, meas=%i\n", tries, setting, meas);
2029 if (meas < (target - tolerance) || meas > (target + tolerance) || tries < 3)
2030 setting = setting * target / meas;
2031 else if (tries > 4 && meas > (target - tolerance) && meas < (target + tolerance))
2033 if (setting < settingmin)
2034 setting = settingmin;
2035 else if (setting > settingmax)
2036 setting = settingmax;
2040 o->pmrChan->spsMeasure->enabled = 0;
2041 ast_debug(4, "DONE tries=%d, setting=%f, meas=%f\n", tries, setting, (float)meas);
2042 if (meas < (target - tolerance) || meas > (target + tolerance))
2043 ast_log(LOG_ERROR, "RX CTCSS GAIN ADJUST FAILED.\n");
2045 ast_log(LOG_NOTICE, "RX CTCSS GAIN ADJUST SUCCESS.\n");
2046 o->rxctcssadj = setting;
2050 this file then is included in chan_usbradio.conf
2051 #include /etc/asterisk/usbradio_tune.conf
2053 static void tune_write(struct chan_usbradio_pvt *o)
2057 fp = fopen("/etc/asterisk/usbradio_tune.conf", "w");
2059 if (!strcmp(o->name, "dsp"))
2060 fprintf(fp, "[general]\n");
2062 fprintf(fp, "[%s]\n", o->name);
2064 fprintf(fp, "; name=%s\n", o->name);
2065 fprintf(fp, "; devicenum=%d\n", o->devicenum);
2067 fprintf(fp, "rxmixerset=%d\n", o->rxmixerset);
2068 fprintf(fp, "rxboostset=%d\n", o->rxboostset);
2069 fprintf(fp, "txmixaset=%d\n", o->txmixaset);
2070 fprintf(fp, "txmixbset=%d\n", o->txmixbset);
2072 fprintf(fp, "rxvoiceadj=%f\n", o->rxvoiceadj);
2073 fprintf(fp, "rxctcssadj=%f\n", o->rxctcssadj);
2074 fprintf(fp, "txctcssadj=%d\n", o->txctcssadj);
2076 fprintf(fp, "rxsquelchadj=%d\n", o->rxsquelchadj);
2080 static void mixer_write(struct chan_usbradio_pvt *o)
2082 setamixer(o->devicenum, MIXER_PARAM_MIC_PLAYBACK_SW, 0, 0);
2083 setamixer(o->devicenum, MIXER_PARAM_MIC_PLAYBACK_VOL, 0, 0);
2084 setamixer(o->devicenum, MIXER_PARAM_SPKR_PLAYBACK_SW, 1, 0);
2085 setamixer(o->devicenum, MIXER_PARAM_SPKR_PLAYBACK_VOL,
2086 o->txmixaset * o->spkrmax / 1000,
2087 o->txmixbset * o->spkrmax / 1000);
2088 setamixer(o->devicenum, MIXER_PARAM_MIC_CAPTURE_VOL,
2089 o->rxmixerset * o->micmax / 1000, 0);
2090 setamixer(o->devicenum, MIXER_PARAM_MIC_BOOST, o->rxboostset, 0);
2091 setamixer(o->devicenum, MIXER_PARAM_MIC_CAPTURE_SW, 1, 0);
2094 adjust dsp multiplier to add resolution to tx level adjustment
2096 static void mult_set(struct chan_usbradio_pvt *o)
2099 if (o->pmrChan->spsTxOutA) {
2100 o->pmrChan->spsTxOutA->outputGain =
2101 mult_calc((o->txmixaset * 152) / 1000);
2103 if (o->pmrChan->spsTxOutB) {
2104 o->pmrChan->spsTxOutB->outputGain =
2105 mult_calc((o->txmixbset * 152) / 1000);
2109 * input 0 - 151 outputs are pot and multiplier
2111 static int mult_calc(int value)
2113 const int multx = M_Q8;
2116 pot= ((int)(value / 4) * 4) + 2;
2117 mult = multx - ((multx * (3 - (value % 4))) / (pot + 2));
2121 #define pd(x) ast_debug(4, #x" = %d\n", x)
2122 #define pp(x) ast_debug(4, #x" = %p\n", x)
2123 #define ps(x) ast_debug(4, #x" = %s\n", x)
2124 #define pf(x) ast_debug(4, #x" = %f\n", x)
2127 static void pmrdump(struct chan_usbradio_pvt *o)
2133 ast_debug(4, "odump()\n");
2145 pd(o->rxsquelchadj);
2154 ast_debug(4, "pmrdump()\n");
2156 ast_debug(4, "prxSquelchAdjust=%d\n", *(o->pmrChan->prxSquelchAdjust));
2158 pd(p->rxCarrierPoint);
2159 pd(p->rxCarrierHyst);
2161 pd(p->rxCtcss->relax);
2163 pd(p->rxCtcssIndex);
2169 pd(p->rxDeEmpEnable);
2170 pd(p->rxCenterSlicerEnable);
2171 pd(p->rxCtcssDecodeEnable);
2172 pd(p->rxDcsDecodeEnable);
2175 pd(p->txLimiterEnable);
2176 pd(p->txPreEmpEnable);
2180 pd(p->spsTxOutA->outputGain);
2182 pd(p->spsTxOutB->outputGain);
2189 * grab fields from the config file, init the descriptor and open the device.
2191 static struct chan_usbradio_pvt *store_config(struct ast_config *cfg, char *ctg)
2193 struct ast_variable *v;
2194 struct chan_usbradio_pvt *o;
2195 struct ast_config *cfg1;
2196 struct ast_flags config_flags = { 0 };
2199 traceusb1(" store_config() ctg == NULL\n");
2200 o = &usbradio_default;
2203 if (!(o = ast_calloc(1, sizeof(*o)))){
2206 *o = usbradio_default;
2207 /* "general" is also the default thing */
2208 if (strcmp(ctg, "general") == 0) {
2209 o->name = ast_strdup("dsp");
2210 usbradio_active = o->name;
2212 o->name = ast_strdup(ctg);
2215 strcpy(o->mohinterpret, "default");
2216 o->micmax = amixer_max(o->devicenum, MIXER_PARAM_MIC_CAPTURE_VOL);
2217 o->spkrmax = amixer_max(o->devicenum, MIXER_PARAM_SPKR_PLAYBACK_VOL);
2218 /* fill other fields from configuration */
2219 for (v = ast_variable_browse(cfg, ctg); v; v = v->next) {
2221 /* handle jb conf */
2222 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
2224 CV_START(v->name, v->value);
2226 CV_UINT("frags", o->frags);
2227 CV_UINT("queuesize", o->queuesize);
2228 CV_UINT("devicenum", o->devicenum);
2229 CV_UINT("debug", usbradio_debug);
2230 CV_BOOL("rxcpusaver", o->rxcpusaver);
2231 CV_BOOL("txcpusaver", o->txcpusaver);
2232 CV_BOOL("invertptt", o->invertptt);
2233 CV_F("rxdemod", store_rxdemod(o, v->value));
2234 CV_BOOL("txprelim", o->txprelim);;
2235 CV_F("txmixa", store_txmixa(o, v->value));
2236 CV_F("txmixb", store_txmixb(o, v->value));
2237 CV_F("carrierfrom", store_rxcdtype(o, v->value));
2238 CV_F("rxsdtype", store_rxsdtype(o, v->value));
2239 CV_F("rxctcssfreq", store_rxctcssfreq(o, v->value));
2240 CV_F("txctcssfreq", store_txctcssfreq(o, v->value));
2241 CV_F("rxgain", store_rxgain(o, v->value));
2242 CV_BOOL("rxboostset", o->rxboostset);
2243 CV_UINT("rxctcssrelax", o->rxctcssrelax);
2244 CV_F("txtoctype", store_txtoctype(o, v->value));
2245 CV_UINT("hdwtype", o->hdwtype);
2246 CV_UINT("duplex", o->radioduplex);
2251 cfg1 = ast_config_load(config1, config_flags);
2253 o->rxmixerset = 500;
2256 o->rxvoiceadj = 0.5;
2257 o->rxctcssadj = 0.5;
2258 o->txctcssadj = 200;
2259 o->rxsquelchadj = 500;
2260 ast_log(LOG_WARNING, "File %s not found, using default parameters.\n", config1);
2262 for (v = ast_variable_browse(cfg1, ctg); v; v = v->next) {
2264 CV_START(v->name, v->value);
2265 CV_UINT("rxmixerset", o->rxmixerset);
2266 CV_UINT("txmixaset", o->txmixaset);
2267 CV_UINT("txmixbset", o->txmixbset);
2268 CV_F("rxvoiceadj", store_rxvoiceadj(o, v->value));
2269 CV_F("rxctcssadj", store_rxctcssadj(o, v->value));
2270 CV_UINT("txctcssadj", o->txctcssadj);
2271 CV_UINT("rxsquelchadj", o->rxsquelchadj);
2274 ast_config_destroy(cfg1);
2279 if (o == &usbradio_default) /* we are done with the default */
2282 o->lastopen = ast_tvnow(); /* don't leave it 0 or tvdiff may wrap */
2283 o->dsp = ast_dsp_new();
2285 ast_dsp_set_features(o->dsp, DSP_FEATURE_DTMF_DETECT);
2286 ast_dsp_set_digitmode(o->dsp, DSP_DIGITMODE_DTMF | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_RELAXDTMF);
2289 if (o->rxctcssfreq != 0 && o->rxdemod == RX_AUDIO_SPEAKER)
2290 ast_log(LOG_ERROR, "Incompatable Options o->rxctcssfreq=%f and o->rxdemod=speaker\n", o->rxctcssfreq);
2292 if (o->pmrChan == NULL) {
2295 memset(&tChan, 0, sizeof(tChan));
2297 tChan.rxDemod = o->rxdemod;
2298 tChan.rxCdType = o->rxcdtype;
2300 tChan.txMod = o->txprelim;
2302 tChan.txMixA = o->txmixa;
2303 tChan.txMixB = o->txmixb;
2305 tChan.rxCpuSaver = o->rxcpusaver;
2306 tChan.txCpuSaver = o->txcpusaver;
2308 tChan.rxCtcssFreq = o->rxctcssfreq;
2309 tChan.txCtcssFreq = o->txctcssfreq;
2311 o->pmrChan = createPmrChannel(&tChan, FRAME_SIZE);
2313 o->pmrChan->radioDuplex = o->radioduplex;
2315 o->pmrChan->rxCpuSaver = o->rxcpusaver;
2316 o->pmrChan->txCpuSaver = o->txcpusaver;
2318 *(o->pmrChan->prxSquelchAdjust) =
2319 ((999 - o->rxsquelchadj) * 32767) / 1000;
2321 o->pmrChan->spsRx->outputGain = o->rxvoiceadj*M_Q8;
2323 o->pmrChan->txTocType = o->txtoctype;
2325 if ((o->txmixa == TX_OUT_LSD) ||
2326 (o->txmixa == TX_OUT_COMPOSITE) ||
2327 (o->txmixb == TX_OUT_LSD) ||
2328 (o->txmixb == TX_OUT_COMPOSITE)) {
2329 *(o->pmrChan->prxCtcssAdjust) = o->rxctcssadj * M_Q8;
2330 set_txctcss_level(o);
2333 o->pmrChan->rxCtcss->relax = o->rxctcssrelax;
2337 if ((o->txmixa != TX_OUT_VOICE) && (o->txmixb != TX_OUT_VOICE) &&
2338 (o->txmixa != TX_OUT_COMPOSITE) && (o->txmixb != TX_OUT_COMPOSITE))
2339 ast_log(LOG_ERROR, "No txvoice output configured.\n");
2341 if (o->txctcssfreq &&
2342 o->txmixa != TX_OUT_LSD && o->txmixa != TX_OUT_COMPOSITE &&
2343 o->txmixb != TX_OUT_LSD && o->txmixb != TX_OUT_COMPOSITE)
2344 ast_log(LOG_ERROR, "No txtone output configured.\n");
2346 if (o->rxctcssfreq && o->pmrChan->rxCtcssIndex < 0)
2347 ast_log(LOG_ERROR, "Invalid CTCSS Frequency.\n");
2357 /* link into list of devices */
2358 if (o != &usbradio_default) {
2359 o->next = usbradio_default.next;
2360 usbradio_default.next = o;
2365 #if DEBUG_FILETEST == 1
2369 int RxTestIt(struct chan_usbradio_pvt *o)
2371 const int numSamples = SAMPLES_PER_BLOCK;
2372 const int numChannels = 16;
2383 FILE *hInput = NULL, *hOutput = NULL, *hOutputTx = NULL;
2385 i16 iBuff[numSamples * 2 * 6], oBuff[numSamples];
2387 ast_debug(4, "RxTestIt()\n");
2390 pChan->b.txCapture = 1;
2391 pChan->b.rxCapture = 1;
2395 hInput = fopen("/usr/src/xpmr/testdata/rx_in.pcm", "r");
2397 ast_debug(4, " RxTestIt() File Not Found.\n");
2400 hOutput = fopen("/usr/src/xpmr/testdata/rx_debug.pcm", "w");
2402 ast_debug(4, " RxTestIt() Working...\n");
2404 while (!feof(hInput)) {
2405 fread((void *)iBuff, 2, numSamples * 2 * 6, hInput);
2408 txHangTime -= numSamples;
2412 if (pChan->rxCtcss->decode)
2413 txHangTime = (8000 / 1000 * 2000);
2415 if (pChan->rxCtcss->decode && !txEnable) {
2417 /* pChan->inputBlanking = (8000 / 1000 * 200); */
2418 } else if (!pChan->rxCtcss->decode && txEnable) {
2422 PmrRx(pChan, iBuff, oBuff);
2424 fwrite((void *)pChan->prxDebug, 2, numSamples * numChannels, hOutput);
2426 pChan->b.txCapture = 0;
2427 pChan->b.rxCapture = 0;
2434 ast_debug(4, " RxTestIt() Complete.\n");
2440 #include "./xpmr/xpmr.c"
2443 static int load_module(void)
2445 struct ast_config *cfg = NULL;
2447 struct ast_flags config_flags = { 0 };
2449 /* Copy the default jb config over global_jbconf */
2450 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
2452 /* load config file */
2453 if (!(cfg = ast_config_load(config, config_flags))) {
2454 ast_log(LOG_NOTICE, "Unable to load config %s\n", config);
2455 return AST_MODULE_LOAD_DECLINE;
2459 store_config(cfg, ctg);
2460 } while ( (ctg = ast_category_browse(cfg, ctg)) != NULL);
2462 ast_config_destroy(cfg);
2464 if (find_desc(usbradio_active) == NULL) {
2465 ast_log(LOG_NOTICE, "Device %s not found\n", usbradio_active);
2466 /* XXX we could default to 'dsp' perhaps ? */
2467 /* XXX should cleanup allocated memory etc. */
2468 return AST_MODULE_LOAD_FAILURE;
2471 if (ast_channel_register(&usbradio_tech)) {
2472 ast_log(LOG_ERROR, "Unable to register channel type 'usb'\n");
2473 return AST_MODULE_LOAD_FAILURE;
2476 ast_cli_register_multiple(cli_usbradio, sizeof(cli_usbradio) / sizeof(struct ast_cli_entry));
2478 return AST_MODULE_LOAD_SUCCESS;
2482 static int unload_module(void)
2484 struct chan_usbradio_pvt *o;
2486 ast_log(LOG_WARNING, "unload_module() called\n");
2488 ast_channel_unregister(&usbradio_tech);
2489 ast_cli_unregister_multiple(cli_usbradio, sizeof(cli_usbradio) / sizeof(struct ast_cli_entry));
2491 for (o = usbradio_default.next; o; o = o->next) {
2493 ast_log(LOG_WARNING, "destroyPmrChannel() called\n");
2495 destroyPmrChannel(o->pmrChan);
2497 #if DEBUG_CAPTURES == 1
2498 if (frxcapraw) { fclose(frxcapraw); frxcapraw = NULL; }
2499 if (frxcaptrace) { fclose(frxcaptrace); frxcaptrace = NULL; }
2500 if (frxoutraw) { fclose(frxoutraw); frxoutraw = NULL; }
2501 if (ftxcapraw) { fclose(ftxcapraw); ftxcapraw = NULL; }
2502 if (ftxcaptrace) { fclose(ftxcaptrace); ftxcaptrace = NULL; }
2503 if (ftxoutraw) { fclose(ftxoutraw); ftxoutraw = NULL; }
2508 ast_dsp_free(o->dsp);
2510 ast_softhangup(o->owner, AST_SOFTHANGUP_APPUNLOAD);
2511 if (o->owner) /* XXX how ??? */
2513 /* XXX what about the thread ? */
2514 /* XXX what about the memory allocated ? */
2519 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "usb Console Channel Driver");