another round of version tag updates, along with 'show version files' pattern filtering
[asterisk/asterisk.git] / channels / chan_modem_i4l.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * ISDN4Linux TTY Driver
5  * 
6  * Copyright (C) 2001, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdio.h>
15
16 #include <string.h>
17 #include <stdlib.h>
18 #include <errno.h>
19 #include <unistd.h>
20 #include <sys/ioctl.h>
21
22 #include "asterisk.h"
23
24 ASTERISK_FILE_VERSION("$Revision$")
25
26 #include "asterisk/lock.h"
27 #include "asterisk/vmodem.h"
28 #include "asterisk/module.h"
29 #include "asterisk/frame.h"
30 #include "asterisk/logger.h"
31 #include "asterisk/options.h"
32 #include "asterisk/dsp.h"
33 #include "asterisk/callerid.h"
34 #include "asterisk/ulaw.h"
35 #include "asterisk/pbx.h"
36
37 #define STATE_COMMAND   0
38 #define STATE_VOICE     1
39
40 static char *breakcmd = "\0x10\0x14\0x10\0x3";
41
42 static char *desc = "ISDN4Linux Emulated Modem Driver";
43
44 static int usecnt;
45 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
46
47 static char *i4l_idents[] = {
48         /* Identify ISDN4Linux Driver */
49         "Linux ISDN",
50         NULL
51 };
52
53 static int i4l_setdev(struct ast_modem_pvt *p, int dev)
54 {
55         char cmd[80];
56         if ((dev != MODEM_DEV_TELCO) && (dev != MODEM_DEV_TELCO_SPK)) {
57                 ast_log(LOG_WARNING, "ISDN4Linux only supports telco device, not %d.\n", dev);
58                 return -1;
59         } else  /* Convert DEV to our understanding of it */
60                 dev = 2;
61         if (ast_modem_send(p, "AT+VLS?", 0)) {
62                 ast_log(LOG_WARNING, "Unable to select current mode %d\n", dev);
63                 return -1;
64         }
65         if (ast_modem_read_response(p, 5)) {
66                 ast_log(LOG_WARNING, "Unable to select device %d\n", dev);
67                 return -1;
68         }
69         ast_modem_trim(p->response);
70         strncpy(cmd, p->response, sizeof(cmd)-1);
71         if (ast_modem_expect(p, "OK", 5)) {
72                 ast_log(LOG_WARNING, "Modem did not respond properly\n");
73                 return -1;
74         }
75         if (dev == atoi(cmd)) {
76                 /* We're already in the right mode, don't bother changing for fear of
77                    hanging up */
78                 return 0;
79         }
80         snprintf(cmd, sizeof(cmd), "AT+VLS=%d", dev);
81         if (ast_modem_send(p, cmd, 0))  {
82                 ast_log(LOG_WARNING, "Unable to select device %d\n", dev);
83                 return -1;
84         }
85         if (ast_modem_read_response(p, 5)) {
86                 ast_log(LOG_WARNING, "Unable to select device %d\n", dev);
87                 return -1;
88         }
89         ast_modem_trim(p->response);
90         if (strcasecmp(p->response, "VCON") && strcasecmp(p->response, "OK")) {
91                 ast_log(LOG_WARNING, "Unexpected reply: %s\n", p->response);
92                 return -1;
93         }
94         return 0;
95 }
96
97 static int i4l_startrec(struct ast_modem_pvt *p)
98 {
99         if (ast_modem_send(p, "AT+VRX+VTX", 0) ||
100              ast_modem_expect(p, "CONNECT", 5)) {
101                 ast_log(LOG_WARNING, "Unable to start recording\n");
102                 return -1;
103         }
104         p->ministate = STATE_VOICE;
105         
106         /*  let ast dsp detect dtmf */
107         if (p->dtmfmode & MODEM_DTMF_AST) {
108                 if (p->dsp) {
109                         ast_log(LOG_DEBUG, "Already have a dsp on %s?\n", p->dev);
110                 } else {
111                         p->dsp = ast_dsp_new();
112                         if (p->dsp) {
113                                 ast_log(LOG_DEBUG, "Detecting DTMF inband with sw DSP on %s\n",p->dev);
114                                 ast_dsp_set_features(p->dsp, DSP_FEATURE_DTMF_DETECT|DSP_FEATURE_FAX_DETECT);
115                                 ast_dsp_digitmode(p->dsp, DSP_DIGITMODE_DTMF | 0);
116                         }
117                 }
118         }
119
120         return 0;
121 }
122
123 static int i4l_break(struct ast_modem_pvt *p)
124 {
125         if (ast_modem_send(p, breakcmd, 2)) {
126                 ast_log(LOG_WARNING, "Failed to break\n");
127                 return -1;
128         }
129         if (ast_modem_send(p, "\r\n", 2)) {
130                 ast_log(LOG_WARNING, "Failed to send enter?\n");
131                 return -1;
132         }
133 #if 0
134         /* Read any outstanding junk */
135         while(!ast_modem_read_response(p, 1));
136 #endif
137         if (ast_modem_send(p, "AT", 0)) {
138                 /* Modem might be stuck in some weird mode, try to get it out */
139                 ast_modem_send(p, "+++", 3);
140                 if (ast_modem_expect(p, "OK", 10)) {
141                         ast_log(LOG_WARNING, "Modem is not responding\n");
142                         return -1;
143                 }
144                 if (ast_modem_send(p, "AT", 0)) {
145                         ast_log(LOG_WARNING, "Modem is not responding\n");
146                         return -1;
147                 }
148         }
149         if (ast_modem_expect(p, "OK", 5)) {
150                 ast_log(LOG_WARNING, "Modem did not respond properly\n");
151                 return -1;
152         }
153         return 0;
154 }
155
156 static int i4l_init(struct ast_modem_pvt *p)
157 {
158         char cmd[256];
159         if (option_debug)
160                 ast_log(LOG_DEBUG, "i4l_init()\n");
161         if (i4l_break(p))
162                 return -1;
163         /* Force into command mode */
164         p->ministate = STATE_COMMAND;
165         if (ast_modem_send(p, "AT+FCLASS=8", 0) ||
166              ast_modem_expect(p, "OK", 5)) {
167                 ast_log(LOG_WARNING, "Unable to set to voice mode\n");
168                 return -1;
169         }
170         if (strlen(p->msn)) {
171                 snprintf(cmd, sizeof(cmd), "AT&E%s", p->msn);
172                 if (ast_modem_send(p, cmd, 0) ||
173                     ast_modem_expect(p, "OK", 5)) {
174                         ast_log(LOG_WARNING, "Unable to set MSN to %s\n", p->msn);
175                         return -1;
176                 }
177         }
178         if (strlen(p->incomingmsn)) {
179                 char *q;
180                 snprintf(cmd, sizeof(cmd), "AT&L%s", p->incomingmsn);
181                 /* translate , into ; since that is the seperator I4L uses, but can't be directly */
182                 /* put in the config file because it will interpret the rest of the line as comment. */
183                 q = cmd+4;
184                 while (*q) {
185                         if (*q == ',') *q = ';';
186                         ++q;
187                 }
188                 if (ast_modem_send(p, cmd, 0) ||
189                     ast_modem_expect(p, "OK", 5)) {
190                         ast_log(LOG_WARNING, "Unable to set Listen to %s\n", p->msn);
191                         return -1;
192                 }
193         }
194         if (ast_modem_send(p, "AT&D2", 0) ||
195              ast_modem_expect(p, "OK", 5)) {
196                 ast_log(LOG_WARNING, "Unable to set to DTR disconnect mode\n");
197                 return -1;
198         }
199         if (ast_modem_send(p, "ATS18=1", 0) ||
200              ast_modem_expect(p, "OK", 5)) {
201                 ast_log(LOG_WARNING, "Unable to set to audio only mode\n");
202                 return -1;
203         }
204         if (ast_modem_send(p, "ATS13.6=1", 0) ||
205              ast_modem_expect(p, "OK", 5)) {
206                 ast_log(LOG_WARNING, "Unable to set to RUNG indication\n");
207                 return -1;
208         }
209         if (ast_modem_send(p, "ATS14=4", 0) ||
210              ast_modem_expect(p, "OK", 5)) {
211                 ast_log(LOG_WARNING, "Unable to set to transparent mode\n");
212                 return -1;
213         }
214         if (ast_modem_send(p, "ATS23=9", 0) ||
215              ast_modem_expect(p, "OK", 5)) {
216                 ast_log(LOG_WARNING, "Unable to set to transparent/ringing mode\n");
217                 return -1;
218         }
219
220         if (ast_modem_send(p, "AT+VSM=6", 0) ||
221              ast_modem_expect(p, "OK", 5)) {
222                 ast_log(LOG_WARNING, "Unable to set to muLAW mode\n");
223                 return -1;
224         }
225         if (ast_modem_send(p, "AT+VLS=2", 0) ||
226              ast_modem_expect(p, "OK", 5)) {
227                 ast_log(LOG_WARNING, "Unable to set to phone line interface\n");
228                 return -1;
229         }
230         p->escape = 0;
231         return 0;
232 }
233
234 static struct ast_frame *i4l_handle_escape(struct ast_modem_pvt *p, char esc)
235 {
236         /* Handle escaped characters -- but sometimes we call it directly as 
237            a quick way to cause known responses */
238         p->fr.frametype = AST_FRAME_NULL;
239         p->fr.subclass = 0;
240         p->fr.data = NULL;
241         p->fr.datalen = 0;
242         p->fr.samples = 0;
243         p->fr.offset = 0;
244         p->fr.mallocd = 0;
245         p->fr.delivery.tv_sec = 0;
246         p->fr.delivery.tv_usec = 0;
247         if (esc && option_debug)
248                 ast_log(LOG_DEBUG, "Escaped character '%c'\n", esc);
249         
250         switch(esc) {
251         case 'R': /* Pseudo ring */
252                 p->fr.frametype = AST_FRAME_CONTROL;
253                 p->fr.subclass = AST_CONTROL_RING;
254                 return &p->fr;
255         case 'I': /* Pseudo ringing */
256                 p->fr.frametype = AST_FRAME_CONTROL;
257                 p->fr.subclass =  AST_CONTROL_RINGING;
258                 return &p->fr;
259         case 'X': /* Pseudo connect */
260                 p->fr.frametype = AST_FRAME_CONTROL;
261                 p->fr.subclass = AST_CONTROL_ANSWER;
262                 if (p->owner)
263                         ast_setstate(p->owner, AST_STATE_UP);
264                 if (i4l_startrec(p))
265                         return  NULL;
266                 return &p->fr;
267         case 'b': /* Busy signal */
268                 p->fr.frametype = AST_FRAME_CONTROL;
269                 p->fr.subclass = AST_CONTROL_BUSY;
270                 return &p->fr;
271         case 'o': /* Overrun */
272                 ast_log(LOG_WARNING, "Overflow on modem, flushing buffers\n");
273                 if (ast_modem_send(p, "\0x10E", 2)) 
274                         ast_log(LOG_WARNING, "Unable to flush buffers\n");
275                 return &p->fr;  
276         case CHAR_ETX: /* End Transmission */
277                 return NULL;
278         case 'u': /* Underrun */
279                 ast_log(LOG_WARNING, "Data underrun\n");
280                 /* Fall Through */
281         case 'd': /* Dialtone */
282         case 'c': /* Calling Tone */
283         case 'e': /* European version */
284         case 'a': /* Answer Tone */
285         case 'f': /* Bell Answer Tone */
286         case 'T': /* Timing mark */
287         case 't': /* Handset off hook */
288         case 'h': /* Handset hungup */
289                 /* Ignore */
290                 if (option_debug)
291                         ast_log(LOG_DEBUG, "Ignoring Escaped character '%c' (%d)\n", esc, esc);
292                 return &p->fr;
293         case '0':
294         case '1':
295         case '2':
296         case '3':
297         case '4':
298         case '5':
299         case '6':
300         case '7':
301         case '8':
302         case '9':
303         case '*':
304         case '#':
305                 ast_log(LOG_DEBUG, "Detected outband DTMF digit: '%c' (%d)\n", esc, esc);
306                 p->fr.frametype=AST_FRAME_DTMF;
307                 p->fr.subclass=esc;
308                 return &p->fr;
309         case 0: /* Pseudo signal */
310                 return &p->fr;
311         default:
312                 ast_log(LOG_DEBUG, "Unknown Escaped character '%c' (%d)\n", esc, esc);
313         }
314         return &p->fr;
315 }
316
317 static struct ast_frame *i4l_read(struct ast_modem_pvt *p)
318 {
319         unsigned char result[256];
320         short *b;
321         struct ast_frame *f=NULL;
322         int res;
323         int x;
324         if (p->ministate == STATE_COMMAND) {
325                 /* Read the first two bytes, first, in case it's a control message */
326                 res = read(p->fd, result, 2);
327                 if (res < 2) {
328                         /* short read, means there was a hangup? */
329                         /* (or is this also possible without hangup?) */
330                         /* Anyway, reading from unitialized buffers is a bad idea anytime. */
331                         if (errno == EAGAIN)
332                                 return i4l_handle_escape(p, 0);
333                         return NULL;
334                 }
335                 if (result[0] == CHAR_DLE) {
336                         return i4l_handle_escape(p, result[1]);
337                         
338                 } else {
339                         if ((result[0] == '\n') || (result[0] == '\r'))
340                                 return i4l_handle_escape(p, 0);
341                         /* Read the rest of the line */
342                         fgets(result + 2, sizeof(result) - 2, p->f);
343                         ast_modem_trim(result);
344                         if (!strcasecmp(result, "VCON")) {
345                                 /* If we're in immediate mode, reply now */
346 /*                              if (p->mode == MODEM_MODE_IMMEDIATE) */
347                                         return i4l_handle_escape(p, 'X');
348                         } else
349                         if (!strcasecmp(result, "BUSY")) {
350                                 /* Same as a busy signal */
351                                 return i4l_handle_escape(p, 'b');
352                         } else
353                         if (!strncasecmp(result, "CALLER NUMBER: ", 15 )) {
354                                 strncpy(p->cid_num, result + 15, sizeof(p->cid_num)-1);
355                                 return i4l_handle_escape(p, 0);
356                         } else
357                         if (!strcasecmp(result, "RINGING")) {
358                                 if (option_verbose > 2)
359                                         ast_verbose(VERBOSE_PREFIX_3 "%s is ringing...\n", p->dev);
360                                 return i4l_handle_escape(p, 'I');
361                         } else
362                         if (!strncasecmp(result, "RUNG", 4)) {
363                                 /* PM2002: the line was hung up before we picked it up, bye bye */
364                                 if (option_verbose > 2) 
365                                         ast_verbose(VERBOSE_PREFIX_3 "%s was hung up on before we answered\n", p->dev);
366                                 return NULL;
367                         } else
368                         if (!strncasecmp(result, "RING", 4)) {
369                                 if (result[4]=='/') 
370                                         strncpy(p->dnid, result + 5, sizeof(p->dnid)-1);
371                                 return i4l_handle_escape(p, 'R');
372                         } else
373                         if (!strcasecmp(result, "NO CARRIER")) {
374                                 if (option_verbose > 2) 
375                                         ast_verbose(VERBOSE_PREFIX_3 "%s hung up on\n", p->dev);
376                                 return NULL;
377                         } else
378                         if (!strcasecmp(result, "NO DIALTONE")) {
379                                 /* There's no dialtone, so the line isn't working */
380                                 ast_log(LOG_WARNING, "Device '%s' lacking dialtone\n", p->dev);
381                                 return NULL;
382                         }
383                         if (option_debug)
384                                 ast_log(LOG_DEBUG, "Modem said '%s'\n", result);
385                         return i4l_handle_escape(p, 0);
386                 }
387         } else {
388                 /* We have to be more efficient in voice mode */
389                 b = (short *)(p->obuf + p->obuflen);
390                 while (p->obuflen/2 < 240) {
391                         /* Read ahead the full amount */
392                         res = read(p->fd, result, 240 - p->obuflen/2);
393                         if (res < 1) {
394                                 /* If there's nothing there, just continue on */
395                                 if (errno == EAGAIN)
396                                         return i4l_handle_escape(p, 0);
397                                 ast_log(LOG_WARNING, "Read failed: %s\n", strerror(errno));
398                                 return NULL;
399                         }
400                         
401                         for (x=0;x<res;x++) {
402                                 /* Process all the bytes that we've read */
403                                 switch(result[x]) {
404                                 case CHAR_DLE:
405 #if 0
406                                         ast_log(LOG_DEBUG, "Ooh, an escape at %d...\n", x);
407 #endif
408                                         if (!p->escape) {
409                                                 /* Note that next value is
410                                                    an escape, and continue. */
411                                                 p->escape++;
412                                                 break;
413                                         } else {
414                                                 /* Send as is -- fallthrough */
415                                                 p->escape = 0;
416                                         }
417                                 default:
418                                         if (p->escape) {
419                                                 ast_log(LOG_DEBUG, "Value of escape is %c (%d)...\n", result[x] < 32 ? '^' : result[x], result[x]);
420                                                 p->escape = 0;
421                                                 if (f) 
422                                                         ast_log(LOG_WARNING, "Warning: Dropped a signal frame\n");
423                                                 f = i4l_handle_escape(p, result[x]);
424                                                 /* If i4l_handle_escape says NULL, say it now, doesn't matter
425                                                 what else is there, the connection is dead. */
426                                                 if (!f)
427                                                         return NULL;
428                                         } else {
429                                                 *(b++) = AST_MULAW(result[x]);
430                                                 p->obuflen += 2;
431                                         }
432                                 }
433                         }
434                         if (f)
435                                 break;
436                 }
437                 if (f) {
438                         if( ! (!(p->dtmfmode & MODEM_DTMF_I4L) && f->frametype == AST_FRAME_DTMF))
439                         return f;
440                 }
441
442                 /* If we get here, we have a complete voice frame */
443                 p->fr.frametype = AST_FRAME_VOICE;
444                 p->fr.subclass = AST_FORMAT_SLINEAR;
445                 p->fr.samples = 240;
446                 p->fr.data = p->obuf;
447                 p->fr.datalen = p->obuflen;
448                 p->fr.mallocd = 0;
449                 p->fr.delivery.tv_sec = 0;
450                 p->fr.delivery.tv_usec = 0;
451                 p->fr.offset = AST_FRIENDLY_OFFSET;
452                 p->fr.src = __FUNCTION__;
453                 p->obuflen = 0;
454
455                 /* process with dsp */
456                 if (p->dsp) {
457                         f = ast_dsp_process(p->owner, p->dsp, &p->fr);
458                         if (f && (f->frametype == AST_FRAME_DTMF)) {
459                                 ast_log(LOG_DEBUG, "Detected inband DTMF digit: %c on %s\n", f->subclass, p->dev);
460                                 if (f->subclass == 'f') {
461                                         /* Fax tone -- Handle and return NULL */
462                                         struct ast_channel *ast = p->owner;
463                                         if (!p->faxhandled) {
464                                                 p->faxhandled++;
465                                                 if (strcmp(ast->exten, "fax")) {
466                                                         const char *target_context = ast_strlen_zero(ast->macrocontext) ? ast->context : ast->macrocontext;
467                                                         
468                                                         if (ast_exists_extension(ast, target_context, "fax", 1, ast->cid.cid_num)) {
469                                                                 if (option_verbose > 2)
470                                                                         ast_verbose(VERBOSE_PREFIX_3 "Redirecting %s to fax extension\n", ast->name);
471                                                                 /* Save the DID/DNIS when we transfer the fax call to a "fax" extension */
472                                                                 pbx_builtin_setvar_helper(ast, "FAXEXTEN", ast->exten);
473                                                                 if (ast_async_goto(ast, target_context, "fax", 1))
474                                                                         ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast->name, target_context);
475                                                         } else
476                                                                 ast_log(LOG_NOTICE, "Fax detected, but no fax extension\n");
477                                                 } else
478                                                         ast_log(LOG_DEBUG, "Already in a fax extension, not redirecting\n");
479                                         } else
480                                                 ast_log(LOG_DEBUG, "Fax already handled\n");
481                                         p->fr.frametype = AST_FRAME_NULL;
482                                         p->fr.subclass = 0;
483                                         f = &p->fr;
484                                 }
485                                 return f;
486                         }
487                 }
488                 
489                 return &p->fr;
490         }
491         return NULL;
492 }
493
494 static int i4l_write(struct ast_modem_pvt *p, struct ast_frame *f)
495 {
496 #define MAX_WRITE_SIZE 2048
497         unsigned char result[MAX_WRITE_SIZE << 1];
498         unsigned char b;
499         int bpos=0, x;
500         int res;
501         if (f->datalen > MAX_WRITE_SIZE) {
502                 ast_log(LOG_WARNING, "Discarding too big frame of size %d\n", f->datalen);
503                 return -1;
504         }
505         if (f->frametype != AST_FRAME_VOICE) {
506                 ast_log(LOG_WARNING, "Don't know how to handle %d type frames\n", f->frametype);
507                 return -1;
508         }
509         if (f->subclass != AST_FORMAT_SLINEAR) {
510                 ast_log(LOG_WARNING, "Don't know how to handle anything but signed linear frames\n");
511                 return -1;
512         }
513         for (x=0;x<f->datalen/2;x++) {
514                 b = AST_LIN2MU(((short *)f->data)[x]);
515                 result[bpos++] = b;
516                 if (b == CHAR_DLE)
517                         result[bpos++]=b;
518         }
519 #if 0
520         res = fwrite(result, bpos, 1, p->f);
521         res *= bpos;
522 #else
523         res = write(p->fd, result, bpos);
524 #endif
525         if (res < 1) {
526                 if (errno != EAGAIN) {
527                         ast_log(LOG_WARNING, "Failed to write buffer\n");
528                         return -1;
529                 }
530         }
531 #if 0
532         printf("Result of write is %d\n", res);
533 #endif
534         return 0;
535 }
536
537 static char *i4l_identify(struct ast_modem_pvt *p)
538 {
539         return strdup("Linux ISDN");
540 }
541
542 static void i4l_incusecnt(void)
543 {
544         ast_mutex_lock(&usecnt_lock);
545         usecnt++;
546         ast_mutex_unlock(&usecnt_lock);
547         ast_update_use_count();
548 }
549
550 static void i4l_decusecnt(void)
551 {
552         ast_mutex_lock(&usecnt_lock);
553         usecnt++;
554         ast_mutex_unlock(&usecnt_lock);
555         ast_update_use_count();
556 }
557
558 static int i4l_answer(struct ast_modem_pvt *p)
559 {
560         if (ast_modem_send(p, "ATA\r", 4) ||
561              ast_modem_expect(p, "VCON", 10)) {
562                 ast_log(LOG_WARNING, "Unable to answer: %s", p->response);
563                 return -1;
564         }
565 #if 1
566         if (ast_modem_send(p, "AT+VDD=0,8", 0) ||
567              ast_modem_expect(p, "OK", 5)) {
568                 ast_log(LOG_WARNING, "Unable to set to phone line interface\n");
569                 return -1;
570         }
571 #endif
572         if (ast_modem_send(p, "AT+VTX+VRX", 0) ||
573              ast_modem_expect(p, "CONNECT", 10)) {
574                 ast_log(LOG_WARNING, "Unable to answer: %s", p->response);
575                 return -1;
576         }
577         p->ministate = STATE_VOICE;
578
579         /*  let ast dsp detect dtmf */
580         if (p->dtmfmode & MODEM_DTMF_AST) {
581                 if (p->dsp) {
582                         ast_log(LOG_DEBUG, "Already have a dsp on %s?\n", p->dev);
583                 } else {
584                         p->dsp = ast_dsp_new();
585                         if (p->dsp) {
586                                 ast_log(LOG_DEBUG, "Detecting DTMF inband with sw DSP on %s\n",p->dev);
587                                 ast_dsp_set_features(p->dsp, DSP_FEATURE_DTMF_DETECT|DSP_FEATURE_FAX_DETECT);
588                                 ast_dsp_digitmode(p->dsp, DSP_DIGITMODE_DTMF | 0);
589                         }
590                 }
591         }
592
593         return 0;
594 }
595
596 static int i4l_dialdigit(struct ast_modem_pvt *p, char digit)
597 {
598         char c[2];
599         if (p->ministate == STATE_VOICE) {
600                 if (p->dtmfmodegen & MODEM_DTMF_I4L) {
601                 c[0] = CHAR_DLE;
602                 c[1] = digit;
603                 write(p->fd, c, 2);
604                         ast_log(LOG_DEBUG, "Send ISDN out-of-band DTMF %c\n",digit);
605                 }
606                 if(p->dtmfmodegen & MODEM_DTMF_AST) {
607                         ast_log(LOG_DEBUG, "Generating inband DTMF\n");
608                         return -1;
609                 }
610         } else
611                 ast_log(LOG_DEBUG, "Asked to send digit but call not up on %s\n", p->dev);
612         return 0;
613 }
614
615 static int i4l_dial(struct ast_modem_pvt *p, char *stuff)
616 {
617         char cmd[80];
618         char tmpmsn[255];
619         struct ast_channel *c = p->owner;
620
621         /* Find callerid number first, to set the correct A number */
622         if (c && c->cid.cid_num && !(c->cid.cid_pres & 0x20)) {
623             snprintf(tmpmsn, sizeof(tmpmsn), ",%s,", c->cid.cid_num);
624             if(strlen(p->outgoingmsn) && strstr(p->outgoingmsn,tmpmsn) != NULL) {
625               /* Tell ISDN4Linux to use this as A number */
626               snprintf(cmd, sizeof(cmd), "AT&E%s\n", c->cid.cid_num);
627               if (ast_modem_send(p, cmd, strlen(cmd))) {
628                 ast_log(LOG_WARNING, "Unable to set A number to %s\n", c->cid.cid_num);
629               }
630
631             } else {
632               ast_log(LOG_WARNING, "Outgoing MSN %s not allowed (see outgoingmsn=%s in modem.conf)\n",c->cid.cid_num,p->outgoingmsn);
633             }
634         }
635
636         snprintf(cmd, sizeof(cmd), "ATD%c %s\n", p->dialtype,stuff);
637         if (ast_modem_send(p, cmd, strlen(cmd))) {
638                 ast_log(LOG_WARNING, "Unable to dial\n");
639                 return -1;
640         }
641         return 0;
642 }
643
644 static int i4l_hangup(struct ast_modem_pvt *p)
645 {
646         char dummy[50];
647         int dtr = TIOCM_DTR;
648
649         /* free the memory used by the DSP */
650         if (p->dsp) {
651                 ast_dsp_free(p->dsp);
652                 p->dsp = NULL;
653         }
654
655         /* down DTR to hangup modem */
656         ioctl(p->fd, TIOCMBIC, &dtr);
657         /* Read anything outstanding */
658         while(read(p->fd, dummy, sizeof(dummy)) > 0);
659
660         /* rise DTR to re-enable line */
661         ioctl(p->fd, TIOCMBIS, &dtr);
662         
663         /* Read anything outstanding */
664         while(read(p->fd, dummy, sizeof(dummy)) > 0);
665
666         /* basically we're done, just to be sure */
667         write(p->fd, "\n\n", 2);
668         read(p->fd, dummy, sizeof(dummy));
669         if (ast_modem_send(p, "ATH", 0)) {
670                 ast_log(LOG_WARNING, "Unable to hang up\n");
671                 return -1;
672         }
673         if (ast_modem_expect(p, "OK", 5)) {
674                 ast_log(LOG_WARNING, "Final 'OK' not received\n");
675                 return -1;
676         }
677
678         return 0;
679 }
680
681 static struct ast_modem_driver i4l_driver =
682 {
683         "i4l",
684         i4l_idents,
685         AST_FORMAT_SLINEAR,
686         0,              /* Not full duplex */
687         i4l_incusecnt,  /* incusecnt */
688         i4l_decusecnt,  /* decusecnt */
689         i4l_identify,   /* identify */
690         i4l_init,       /* init */
691         i4l_setdev,     /* setdev */
692         i4l_read,
693         i4l_write,
694         i4l_dial,       /* dial */
695         i4l_answer,     /* answer */
696         i4l_hangup,     /* hangup */
697         i4l_startrec,   /* start record */
698         NULL,   /* stop record */
699         NULL,   /* start playback */
700         NULL,   /* stop playback */
701         NULL,   /* set silence supression */
702         i4l_dialdigit,  /* dialdigit */
703 };
704
705
706
707 int usecount(void)
708 {
709         int res;
710         ast_mutex_lock(&usecnt_lock);
711         res = usecnt;
712         ast_mutex_unlock(&usecnt_lock);
713         return res;
714 }
715
716 int load_module(void)
717 {
718         return ast_register_modem_driver(&i4l_driver);
719 }
720
721 int unload_module(void)
722 {
723         return ast_unregister_modem_driver(&i4l_driver);
724 }
725
726 char *description()
727 {
728         return desc;
729 }
730
731 char *key()
732 {
733         return ASTERISK_GPL_KEY;
734 }