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 $
20 #include <sys/ioccom.h>
23 #include <sys/types.h>
24 #include <sys/ioctl.h>
28 #include <machine/i4b_tel_ioctl.h>
30 static void create_session(void);
31 static void input_byte(int byte, int stopbit);
32 static void sample(int vol, int* tones);
33 static void tonedetect(unsigned char *ptr, int count);
34 static void uart(int bit);
36 static int dcd; /* Carrier on ? */
37 static int ptyfd = -1; /* PTY filedescriptor */
38 static int telfd = -1; /* I4BTEL filedescriptor */
41 * Alaw to Linear [-32767..32767] conversion
44 static int a2l[256] = { -5504, -5248, -6016, -5760, -4480, -4224,
45 -4992, -4736, -7552, -7296, -8064, -7808, -6528, -6272, -7040,
46 -6784, -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
47 -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392, -22016,
48 -20992, -24064, -23040, -17920, -16896, -19968, -18944, -30208,
49 -29184, -32256, -31232, -26112, -25088, -28160, -27136, -11008,
50 -10496, -12032, -11520, -8960, -8448, -9984, -9472, -15104, -14592,
51 -16128, -15616, -13056, -12544, -14080, -13568, -344, -328, -376,
52 -360, -280, -264, -312, -296, -472, -456, -504, -488, -408, -392,
53 -440, -424, -88, -72, -120, -104, -24, -8, -56, -40, -216, -200,
54 -248, -232, -152, -136, -184, -168, -1376, -1312, -1504, -1440,
55 -1120, -1056, -1248, -1184, -1888, -1824, -2016, -1952, -1632,
56 -1568, -1760, -1696, -688, -656, -752, -720, -560, -528, -624,
57 -592, -944, -912, -1008, -976, -816, -784, -880, -848, 5504, 5248,
58 6016, 5760, 4480, 4224, 4992, 4736, 7552, 7296, 8064, 7808, 6528,
59 6272, 7040, 6784, 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
60 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392, 22016, 20992, 24064,
61 23040, 17920, 16896, 19968, 18944, 30208, 29184, 32256, 31232,
62 26112, 25088, 28160, 27136, 11008, 10496, 12032, 11520, 8960, 8448,
63 9984, 9472, 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
64 344, 328, 376, 360, 280, 264, 312, 296, 472, 456, 504, 488, 408,
65 392, 440, 424, 88, 72, 120, 104, 24, 8, 56, 40, 216, 200, 248, 232,
66 152, 136, 184, 168, 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
67 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696, 688, 656, 752, 720,
68 560, 528, 624, 592, 944, 912, 1008, 976, 816, 784, 880, 848 };
71 * A High Q Tone detector
74 #define NTONES 2 /* Number of tones to detect */
75 #define SCALE 4096 /* Scaling factor */
76 #define EXPAVG 14 /* Exponential Average factor */
77 #define POLERAD 3885 /* pole_radius ^ 2 * SCALE */
79 /* Table of "-cos(2 * PI * frequency / sample_rate) * SCALE" */
80 static int p[NTONES] = {
86 tonedetect(unsigned char *ptr, int count)
91 static int k[NTONES], h[NTONES];
92 static int tones[NTONES];
96 for (i = 0; i < count; i++) {
99 amplitude += (y - amplitude) / EXPAVG;
101 amplitude += (-y - amplitude) / EXPAVG;
103 for(j = 0; j < NTONES; j++) {
104 c = (POLERAD * (y - k[j])) / SCALE;
106 f = (p[j] * (d - h[j])) / SCALE;
112 tones[j] += (n - tones[j]) / EXPAVG;
114 sample(amplitude, tones);
119 * Taste each sample, detect (loss off) carrier, and feed uart
122 #define NCARRIER 1000 /* Samples of carrier for detection */
125 sample(int vol, int* tones)
129 if ((tones[0] + tones[1]) > vol * 3/2) { /* XXX */
130 if (carrier < NCARRIER)
137 if (!dcd && carrier > NCARRIER / 2) {
138 syslog(LOG_ERR, "CARRIER ON");
140 } else if (dcd && carrier < NCARRIER / 2) {
141 syslog(LOG_ERR, "CARRIER OFF");
148 if (tones[0] > tones[1]) {
159 #define BITCENTER 13 /* Middle of a bit: 8000/300/2 */
160 static int bitsample[] = { /* table of sampling points */
178 if (n == 0 && bit == 1)
179 return; /* Waiting for start bit */
181 j = 0; /* Begin start bit */
184 } else if (j == 0 && bit && n > bitsample[j]) {
185 n = 0; /* Gone by middle of start bit */
186 } else if (n > bitsample[j]) {
187 j++; /* Sample point */
192 v = v / 2 + 128 * bit;
201 * Send a byte using kenrnel tone generation support
207 struct i4b_tel_tones tt;
211 tt.frequency[i] = 1850; tt.duration[i++] = 27;
213 tt.frequency[i] = val & 1 ? 1650 : 1850; tt.duration[i++] = 27;
214 tt.frequency[i] = val & 2 ? 1650 : 1850; tt.duration[i++] = 26;
215 tt.frequency[i] = val & 4 ? 1650 : 1850; tt.duration[i++] = 27;
216 tt.frequency[i] = val & 8 ? 1650 : 1850; tt.duration[i++] = 27;
217 tt.frequency[i] = val & 16 ? 1650 : 1850; tt.duration[i++] = 26;
218 tt.frequency[i] = val & 32 ? 1650 : 1850; tt.duration[i++] = 27;
219 tt.frequency[i] = val & 64 ? 1650 : 1850; tt.duration[i++] = 27;
220 tt.frequency[i] = val & 128 ? 1650 : 1850; tt.duration[i++] = 26;
222 tt.frequency[i] = 1650; tt.duration[i++] = 27;
223 tt.frequency[i] = 1650; tt.duration[i++] = 0;
225 i = ioctl(telfd, I4B_TEL_TONES, &tt);
226 if (i != 0 && errno != EAGAIN) {
227 syslog(LOG_ERR, "%d: *** %d/%d ***", __LINE__, i, errno);
242 i = forkpty(&ptyfd, buf, 0, 0);
244 execl("/usr/libexec/getty", "getty", "std.300", "-",
246 syslog(LOG_ERR, "exec getty %d", errno);
249 syslog(LOG_ERR, "forkpty failed %d", errno);
252 syslog(LOG_ERR, "pty %s", buf);
256 input_byte(int byte, int stopbit)
261 static u_char buf[80];
267 * I have no idea why, but my TB2500 modem sends a sequence of
268 * 28 bytes after carrier is established at the link level, but
269 * before it is acceptted at the logical level.
271 * [16100214010201060100000000ff0201020301080402400010034510]
273 * Unfortunately this contains a ^D which kills getty.
274 * The following code swallows this sequence, assuming that it
275 * is always the same length and always start with 0x16.
278 if (first == 0 && c == 0x16) {
279 sprintf(buf, "%02x", c);
282 } else if (first == 0) {
288 sprintf(buf + strlen(buf), "%02x", c);
291 syslog(LOG_NOTICE, "Got magic [%s]", buf);
296 if (ptyfd != -1 && dcd) {
297 i = write(ptyfd, &c, 1);
298 if (i != 1 && errno != EAGAIN) {
299 syslog(LOG_ERR, "%d: *** %d/%d ***", __LINE__, i, errno);
306 main(int argc, char **argv)
308 char *device = "/dev/tel0";
312 struct i4b_tel_tones tt;
313 fd_set rfd, wfd, efd;
315 openlog("v21modem", LOG_PID, LOG_DAEMON);
316 /* Find our device name */
317 for (i = 0; i < argc; i++)
318 if (!strcmp(argv[i], "-D"))
319 device = argv[i + 1];
320 telfd = open(device, O_RDWR, 0);
322 syslog(LOG_ERR, "open %s: %m", device);
325 syslog(LOG_NOTICE, "Running on %s", device);
327 /* Output V.25 tone and carrier */
329 tt.frequency[i] = 0; tt.duration[i++] = 1000;
330 tt.frequency[i] = 2100; tt.duration[i++] = 2*8000;
331 tt.frequency[i] = 0; tt.duration[i++] = 400;
332 tt.frequency[i] = 1650; tt.duration[i++] = 1;
333 tt.frequency[i] = 1650; tt.duration[i++] = 0;
334 tt.frequency[i] = 0; tt.duration[i++] = 0;
335 i = ioctl(telfd, I4B_TEL_TONES, &tt);
337 syslog(LOG_ERR, "hangup");
343 /* Wait for carrier */
345 ii = read(telfd, ibuf, sizeof ibuf);
346 tonedetect(ibuf, ii);
347 } while (ii > 0 && dcd != 2);
349 syslog(LOG_ERR, "hangup");
365 i = select(maxfd, &rfd, &wfd, &efd, NULL);
366 if (FD_ISSET(telfd, &rfd)) {
367 ii = read(telfd, ibuf, sizeof ibuf);
369 tonedetect(ibuf, ii);
371 syslog(LOG_ERR, "hangup");
373 if (FD_ISSET(ptyfd, &rfd)) {
374 io = read(ptyfd, ibuf, 1);
378 syslog(LOG_ERR, "Session EOF");
383 if (FD_ISSET(telfd, &efd)) {
384 syslog(LOG_ERR, "Exception TELFD");
387 if (FD_ISSET(ptyfd, &efd)) {
388 syslog(LOG_ERR, "Exception PTYFD");
392 syslog(LOG_ERR, "Carrier Lost");