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