2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
9 * This is a V.21 modem for ISDN4BSD.
11 * $FreeBSD: src/share/examples/isdn/v21/v21modem.c,v 1.2.2.1 2001/08/10 14:59:48 obrien Exp $
12 * $DragonFly: src/share/examples/isdn/v21/v21modem.c,v 1.2 2003/06/17 04:36:57 dillon Exp $
21 #include <sys/ioccom.h>
24 #include <sys/types.h>
25 #include <sys/ioctl.h>
29 #include <machine/i4b_tel_ioctl.h>
31 static void create_session(void);
32 static void input_byte(int byte, int stopbit);
33 static void sample(int vol, int* tones);
34 static void tonedetect(unsigned char *ptr, int count);
35 static void uart(int bit);
37 static int dcd; /* Carrier on ? */
38 static int ptyfd = -1; /* PTY filedescriptor */
39 static int telfd = -1; /* I4BTEL filedescriptor */
42 * Alaw to Linear [-32767..32767] conversion
45 static int a2l[256] = { -5504, -5248, -6016, -5760, -4480, -4224,
46 -4992, -4736, -7552, -7296, -8064, -7808, -6528, -6272, -7040,
47 -6784, -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
48 -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392, -22016,
49 -20992, -24064, -23040, -17920, -16896, -19968, -18944, -30208,
50 -29184, -32256, -31232, -26112, -25088, -28160, -27136, -11008,
51 -10496, -12032, -11520, -8960, -8448, -9984, -9472, -15104, -14592,
52 -16128, -15616, -13056, -12544, -14080, -13568, -344, -328, -376,
53 -360, -280, -264, -312, -296, -472, -456, -504, -488, -408, -392,
54 -440, -424, -88, -72, -120, -104, -24, -8, -56, -40, -216, -200,
55 -248, -232, -152, -136, -184, -168, -1376, -1312, -1504, -1440,
56 -1120, -1056, -1248, -1184, -1888, -1824, -2016, -1952, -1632,
57 -1568, -1760, -1696, -688, -656, -752, -720, -560, -528, -624,
58 -592, -944, -912, -1008, -976, -816, -784, -880, -848, 5504, 5248,
59 6016, 5760, 4480, 4224, 4992, 4736, 7552, 7296, 8064, 7808, 6528,
60 6272, 7040, 6784, 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
61 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392, 22016, 20992, 24064,
62 23040, 17920, 16896, 19968, 18944, 30208, 29184, 32256, 31232,
63 26112, 25088, 28160, 27136, 11008, 10496, 12032, 11520, 8960, 8448,
64 9984, 9472, 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
65 344, 328, 376, 360, 280, 264, 312, 296, 472, 456, 504, 488, 408,
66 392, 440, 424, 88, 72, 120, 104, 24, 8, 56, 40, 216, 200, 248, 232,
67 152, 136, 184, 168, 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
68 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696, 688, 656, 752, 720,
69 560, 528, 624, 592, 944, 912, 1008, 976, 816, 784, 880, 848 };
72 * A High Q Tone detector
75 #define NTONES 2 /* Number of tones to detect */
76 #define SCALE 4096 /* Scaling factor */
77 #define EXPAVG 14 /* Exponential Average factor */
78 #define POLERAD 3885 /* pole_radius ^ 2 * SCALE */
80 /* Table of "-cos(2 * PI * frequency / sample_rate) * SCALE" */
81 static int p[NTONES] = {
87 tonedetect(unsigned char *ptr, int count)
92 static int k[NTONES], h[NTONES];
93 static int tones[NTONES];
97 for (i = 0; i < count; i++) {
100 amplitude += (y - amplitude) / EXPAVG;
102 amplitude += (-y - amplitude) / EXPAVG;
104 for(j = 0; j < NTONES; j++) {
105 c = (POLERAD * (y - k[j])) / SCALE;
107 f = (p[j] * (d - h[j])) / SCALE;
113 tones[j] += (n - tones[j]) / EXPAVG;
115 sample(amplitude, tones);
120 * Taste each sample, detect (loss off) carrier, and feed uart
123 #define NCARRIER 1000 /* Samples of carrier for detection */
126 sample(int vol, int* tones)
130 if ((tones[0] + tones[1]) > vol * 3/2) { /* XXX */
131 if (carrier < NCARRIER)
138 if (!dcd && carrier > NCARRIER / 2) {
139 syslog(LOG_ERR, "CARRIER ON");
141 } else if (dcd && carrier < NCARRIER / 2) {
142 syslog(LOG_ERR, "CARRIER OFF");
149 if (tones[0] > tones[1]) {
160 #define BITCENTER 13 /* Middle of a bit: 8000/300/2 */
161 static int bitsample[] = { /* table of sampling points */
179 if (n == 0 && bit == 1)
180 return; /* Waiting for start bit */
182 j = 0; /* Begin start bit */
185 } else if (j == 0 && bit && n > bitsample[j]) {
186 n = 0; /* Gone by middle of start bit */
187 } else if (n > bitsample[j]) {
188 j++; /* Sample point */
193 v = v / 2 + 128 * bit;
202 * Send a byte using kenrnel tone generation support
208 struct i4b_tel_tones tt;
212 tt.frequency[i] = 1850; tt.duration[i++] = 27;
214 tt.frequency[i] = val & 1 ? 1650 : 1850; tt.duration[i++] = 27;
215 tt.frequency[i] = val & 2 ? 1650 : 1850; tt.duration[i++] = 26;
216 tt.frequency[i] = val & 4 ? 1650 : 1850; tt.duration[i++] = 27;
217 tt.frequency[i] = val & 8 ? 1650 : 1850; tt.duration[i++] = 27;
218 tt.frequency[i] = val & 16 ? 1650 : 1850; tt.duration[i++] = 26;
219 tt.frequency[i] = val & 32 ? 1650 : 1850; tt.duration[i++] = 27;
220 tt.frequency[i] = val & 64 ? 1650 : 1850; tt.duration[i++] = 27;
221 tt.frequency[i] = val & 128 ? 1650 : 1850; tt.duration[i++] = 26;
223 tt.frequency[i] = 1650; tt.duration[i++] = 27;
224 tt.frequency[i] = 1650; tt.duration[i++] = 0;
226 i = ioctl(telfd, I4B_TEL_TONES, &tt);
227 if (i != 0 && errno != EAGAIN) {
228 syslog(LOG_ERR, "%d: *** %d/%d ***", __LINE__, i, errno);
243 i = forkpty(&ptyfd, buf, 0, 0);
245 execl("/usr/libexec/getty", "getty", "std.300", "-",
247 syslog(LOG_ERR, "exec getty %d", errno);
250 syslog(LOG_ERR, "forkpty failed %d", errno);
253 syslog(LOG_ERR, "pty %s", buf);
257 input_byte(int byte, int stopbit)
262 static u_char buf[80];
268 * I have no idea why, but my TB2500 modem sends a sequence of
269 * 28 bytes after carrier is established at the link level, but
270 * before it is acceptted at the logical level.
272 * [16100214010201060100000000ff0201020301080402400010034510]
274 * Unfortunately this contains a ^D which kills getty.
275 * The following code swallows this sequence, assuming that it
276 * is always the same length and always start with 0x16.
279 if (first == 0 && c == 0x16) {
280 sprintf(buf, "%02x", c);
283 } else if (first == 0) {
289 sprintf(buf + strlen(buf), "%02x", c);
292 syslog(LOG_NOTICE, "Got magic [%s]", buf);
297 if (ptyfd != -1 && dcd) {
298 i = write(ptyfd, &c, 1);
299 if (i != 1 && errno != EAGAIN) {
300 syslog(LOG_ERR, "%d: *** %d/%d ***", __LINE__, i, errno);
307 main(int argc, char **argv)
309 char *device = "/dev/tel0";
313 struct i4b_tel_tones tt;
314 fd_set rfd, wfd, efd;
316 openlog("v21modem", LOG_PID, LOG_DAEMON);
317 /* Find our device name */
318 for (i = 0; i < argc; i++)
319 if (!strcmp(argv[i], "-D"))
320 device = argv[i + 1];
321 telfd = open(device, O_RDWR, 0);
323 syslog(LOG_ERR, "open %s: %m", device);
326 syslog(LOG_NOTICE, "Running on %s", device);
328 /* Output V.25 tone and carrier */
330 tt.frequency[i] = 0; tt.duration[i++] = 1000;
331 tt.frequency[i] = 2100; tt.duration[i++] = 2*8000;
332 tt.frequency[i] = 0; tt.duration[i++] = 400;
333 tt.frequency[i] = 1650; tt.duration[i++] = 1;
334 tt.frequency[i] = 1650; tt.duration[i++] = 0;
335 tt.frequency[i] = 0; tt.duration[i++] = 0;
336 i = ioctl(telfd, I4B_TEL_TONES, &tt);
338 syslog(LOG_ERR, "hangup");
344 /* Wait for carrier */
346 ii = read(telfd, ibuf, sizeof ibuf);
347 tonedetect(ibuf, ii);
348 } while (ii > 0 && dcd != 2);
350 syslog(LOG_ERR, "hangup");
366 i = select(maxfd, &rfd, &wfd, &efd, NULL);
367 if (FD_ISSET(telfd, &rfd)) {
368 ii = read(telfd, ibuf, sizeof ibuf);
370 tonedetect(ibuf, ii);
372 syslog(LOG_ERR, "hangup");
374 if (FD_ISSET(ptyfd, &rfd)) {
375 io = read(ptyfd, ibuf, 1);
379 syslog(LOG_ERR, "Session EOF");
384 if (FD_ISSET(telfd, &efd)) {
385 syslog(LOG_ERR, "Exception TELFD");
388 if (FD_ISSET(ptyfd, &efd)) {
389 syslog(LOG_ERR, "Exception PTYFD");
393 syslog(LOG_ERR, "Carrier Lost");