Merge ../linux-2.6
[sfrench/cifs-2.6.git] / drivers / isdn / divert / isdn_divert.c
1 /* $Id: isdn_divert.c,v 1.6.6.3 2001/09/23 22:24:36 kai Exp $
2  *
3  * DSS1 main diversion supplementary handling for i4l.
4  *
5  * Copyright 1999       by Werner Cornelius (werner@isdn4linux.de)
6  * 
7  * This software may be used and distributed according to the terms
8  * of the GNU General Public License, incorporated herein by reference.
9  *
10  */
11
12 #include <linux/proc_fs.h>
13
14 #include "isdn_divert.h"
15
16 /**********************************/
17 /* structure keeping calling info */
18 /**********************************/
19 struct call_struc
20   { isdn_ctrl ics; /* delivered setup + driver parameters */
21     ulong divert_id; /* Id delivered to user */
22     unsigned char akt_state; /* actual state */
23     char deflect_dest[35]; /* deflection destination */   
24     struct timer_list timer; /* timer control structure */
25     char info[90]; /* device info output */ 
26     struct call_struc *next; /* pointer to next entry */
27     struct call_struc *prev;
28   };
29
30
31 /********************************************/
32 /* structure keeping deflection table entry */
33 /********************************************/
34 struct deflect_struc
35   { struct deflect_struc *next,*prev; 
36     divert_rule rule; /* used rule */
37   };
38
39
40 /*****************************************/
41 /* variables for main diversion services */
42 /*****************************************/
43 /* diversion/deflection processes */
44 static struct call_struc *divert_head = NULL; /* head of remembered entrys */
45 static ulong next_id = 1; /* next info id */   
46 static struct deflect_struc *table_head = NULL;
47 static struct deflect_struc *table_tail = NULL; 
48 static unsigned char extern_wait_max = 4; /* maximum wait in s for external process */ 
49
50 DEFINE_SPINLOCK(divert_lock);
51
52 /***************************/
53 /* timer callback function */
54 /***************************/
55 static void deflect_timer_expire(ulong arg)
56 {
57   unsigned long flags;
58   struct call_struc *cs = (struct call_struc *) arg;
59
60   spin_lock_irqsave(&divert_lock, flags);
61   del_timer(&cs->timer); /* delete active timer */
62   spin_unlock_irqrestore(&divert_lock, flags);
63
64   switch(cs->akt_state)
65    { case DEFLECT_PROCEED:
66        cs->ics.command = ISDN_CMD_HANGUP; /* cancel action */
67        divert_if.ll_cmd(&cs->ics);                        
68        spin_lock_irqsave(&divert_lock, flags);
69        cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */
70        cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);
71        add_timer(&cs->timer);
72        spin_unlock_irqrestore(&divert_lock, flags);
73        break;
74
75      case DEFLECT_ALERT:
76        cs->ics.command = ISDN_CMD_REDIR; /* protocol */
77        strcpy(cs->ics.parm.setup.phone,cs->deflect_dest);
78        strcpy(cs->ics.parm.setup.eazmsn,"Testtext delayed");
79        divert_if.ll_cmd(&cs->ics);
80        spin_lock_irqsave(&divert_lock, flags);
81        cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */
82        cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);
83        add_timer(&cs->timer);
84        spin_unlock_irqrestore(&divert_lock, flags);
85        break;
86
87      case DEFLECT_AUTODEL:
88      default:
89        spin_lock_irqsave(&divert_lock, flags);
90        if (cs->prev) 
91          cs->prev->next = cs->next; /* forward link */
92         else
93          divert_head = cs->next;
94        if (cs->next)
95          cs->next->prev = cs->prev; /* back link */           
96        spin_unlock_irqrestore(&divert_lock, flags);
97        kfree(cs);
98        return;
99
100    } /* switch */
101 } /* deflect_timer_func */
102
103
104 /*****************************************/
105 /* handle call forwarding de/activations */
106 /* 0 = deact, 1 = act, 2 = interrogate   */
107 /*****************************************/
108 int cf_command(int drvid, int mode, 
109                u_char proc, char *msn, 
110                u_char service, char *fwd_nr, ulong *procid)
111 { unsigned long flags;
112   int retval,msnlen;
113   int fwd_len;
114   char *p,*ielenp,tmp[60];
115   struct call_struc *cs;
116
117   if (strchr(msn,'.')) return(-EINVAL); /* subaddress not allowed in msn */
118   if ((proc & 0x7F) > 2) return(-EINVAL);
119   proc &= 3;
120   p = tmp;
121   *p++ = 0x30; /* enumeration */
122   ielenp = p++; /* remember total length position */
123   *p++ = 0xa; /* proc tag */
124   *p++ = 1;   /* length */
125   *p++ = proc & 0x7F; /* procedure to de/activate/interrogate */
126   *p++ = 0xa; /* service tag */
127   *p++ = 1;   /* length */
128   *p++ = service; /* service to handle */
129
130   if (mode == 1) 
131    { if (!*fwd_nr) return(-EINVAL); /* destination missing */
132      if (strchr(fwd_nr,'.')) return(-EINVAL); /* subaddress not allowed */
133      fwd_len = strlen(fwd_nr);
134      *p++ = 0x30; /* number enumeration */
135      *p++ = fwd_len + 2; /* complete forward to len */ 
136      *p++ = 0x80; /* fwd to nr */
137      *p++ = fwd_len; /* length of number */
138      strcpy(p,fwd_nr); /* copy number */
139      p += fwd_len; /* pointer beyond fwd */
140    } /* activate */
141
142   msnlen = strlen(msn);
143   *p++ = 0x80; /* msn number */
144   if (msnlen > 1)
145    { *p++ = msnlen; /* length */
146      strcpy(p,msn);
147      p += msnlen;
148    }
149   else *p++ = 0;
150
151   *ielenp = p - ielenp - 1; /* set total IE length */ 
152
153   /* allocate mem for information struct */  
154   if (!(cs = (struct call_struc *) kmalloc(sizeof(struct call_struc), GFP_ATOMIC))) 
155              return(-ENOMEM); /* no memory */
156   init_timer(&cs->timer);
157   cs->info[0] = '\0';
158   cs->timer.function = deflect_timer_expire;
159   cs->timer.data = (ulong) cs; /* pointer to own structure */
160   cs->ics.driver = drvid;
161   cs->ics.command = ISDN_CMD_PROT_IO; /* protocol specific io */
162   cs->ics.arg = DSS1_CMD_INVOKE; /* invoke supplementary service */
163   cs->ics.parm.dss1_io.proc = (mode == 1) ? 7: (mode == 2) ? 11:8; /* operation */ 
164   cs->ics.parm.dss1_io.timeout = 4000; /* from ETS 300 207-1 */
165   cs->ics.parm.dss1_io.datalen = p - tmp; /* total len */
166   cs->ics.parm.dss1_io.data = tmp; /* start of buffer */
167   
168   spin_lock_irqsave(&divert_lock, flags);
169   cs->ics.parm.dss1_io.ll_id = next_id++; /* id for callback */
170   spin_unlock_irqrestore(&divert_lock, flags);
171   *procid = cs->ics.parm.dss1_io.ll_id;  
172
173   sprintf(cs->info,"%d 0x%lx %s%s 0 %s %02x %d%s%s\n",
174           (!mode ) ? DIVERT_DEACTIVATE : (mode == 1) ? DIVERT_ACTIVATE : DIVERT_REPORT,
175           cs->ics.parm.dss1_io.ll_id,
176           (mode != 2) ? "" : "0 ",
177           divert_if.drv_to_name(cs->ics.driver),
178           msn,
179           service & 0xFF,
180           proc,
181           (mode != 1) ? "" : " 0 ",
182           (mode != 1) ? "" : fwd_nr);
183  
184   retval = divert_if.ll_cmd(&cs->ics); /* excute command */
185
186   if (!retval)
187    { cs->prev = NULL;
188      spin_lock_irqsave(&divert_lock, flags);
189      cs->next = divert_head;
190      divert_head = cs; 
191      spin_unlock_irqrestore(&divert_lock, flags);
192    }
193   else
194    kfree(cs);
195   return(retval); 
196 } /* cf_command */
197
198
199 /****************************************/
200 /* handle a external deflection command */
201 /****************************************/
202 int deflect_extern_action(u_char cmd, ulong callid, char *to_nr)
203 { struct call_struc *cs;
204   isdn_ctrl ic;
205   unsigned long flags;
206   int i;
207
208   if ((cmd & 0x7F) > 2) return(-EINVAL); /* invalid command */
209   cs = divert_head; /* start of parameter list */
210   while (cs)
211    { if (cs->divert_id == callid) break; /* found */
212      cs = cs->next;  
213    } /* search entry */
214   if (!cs) return(-EINVAL); /* invalid callid */
215
216   ic.driver = cs->ics.driver;
217   ic.arg = cs->ics.arg;
218   i = -EINVAL;
219   if (cs->akt_state == DEFLECT_AUTODEL) return(i); /* no valid call */
220   switch (cmd & 0x7F)
221    { case 0: /* hangup */
222        del_timer(&cs->timer); 
223        ic.command = ISDN_CMD_HANGUP;
224        i = divert_if.ll_cmd(&ic);
225        spin_lock_irqsave(&divert_lock, flags);
226        cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */
227        cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);
228        add_timer(&cs->timer);
229        spin_unlock_irqrestore(&divert_lock, flags);
230      break;      
231
232      case 1: /* alert */
233        if (cs->akt_state == DEFLECT_ALERT) return(0);
234        cmd &= 0x7F; /* never wait */
235        del_timer(&cs->timer); 
236        ic.command = ISDN_CMD_ALERT;
237        if ((i = divert_if.ll_cmd(&ic)))
238         {
239           spin_lock_irqsave(&divert_lock, flags);
240           cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */
241           cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);
242           add_timer(&cs->timer);
243           spin_unlock_irqrestore(&divert_lock, flags);
244         }
245        else
246           cs->akt_state = DEFLECT_ALERT; 
247      break;      
248
249      case 2: /* redir */
250        del_timer(&cs->timer); 
251        strcpy(cs->ics.parm.setup.phone, to_nr);
252        strcpy(cs->ics.parm.setup.eazmsn, "Testtext manual");
253        ic.command = ISDN_CMD_REDIR;
254        if ((i = divert_if.ll_cmd(&ic)))
255         {
256           spin_lock_irqsave(&divert_lock, flags);
257           cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */
258           cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);
259           add_timer(&cs->timer);
260           spin_unlock_irqrestore(&divert_lock, flags);
261         }
262        else
263           cs->akt_state = DEFLECT_ALERT; 
264      break;      
265
266    } /* switch */
267   return(i);
268 } /* deflect_extern_action */
269
270 /********************************/
271 /* insert a new rule before idx */
272 /********************************/
273 int insertrule(int idx, divert_rule *newrule)
274 { struct deflect_struc *ds,*ds1=NULL;
275   unsigned long flags;
276
277   if (!(ds = (struct deflect_struc *) kmalloc(sizeof(struct deflect_struc), 
278                                               GFP_KERNEL))) 
279     return(-ENOMEM); /* no memory */
280
281   ds->rule = *newrule; /* set rule */
282
283   spin_lock_irqsave(&divert_lock, flags);
284
285   if (idx >= 0)
286    { ds1 = table_head;
287      while ((ds1) && (idx > 0))
288       { idx--;
289         ds1 = ds1->next;
290       } 
291      if (!ds1) idx = -1; 
292    }
293
294   if (idx < 0)
295    { ds->prev = table_tail; /* previous entry */
296      ds->next = NULL; /* end of chain */
297      if (ds->prev) 
298        ds->prev->next = ds; /* last forward */
299       else
300         table_head = ds; /* is first entry */
301      table_tail = ds; /* end of queue */
302    }
303   else
304     { ds->next = ds1; /* next entry */
305       ds->prev = ds1->prev; /* prev entry */
306       ds1->prev = ds; /* backward chain old element */
307       if (!ds->prev)
308         table_head = ds; /* first element */
309    }
310
311   spin_unlock_irqrestore(&divert_lock, flags);
312   return(0);
313 } /* insertrule */
314
315 /***********************************/
316 /* delete the rule at position idx */
317 /***********************************/
318 int deleterule(int idx)
319 { struct deflect_struc *ds,*ds1;
320   unsigned long flags;
321   
322   if (idx < 0) 
323    { spin_lock_irqsave(&divert_lock, flags);
324      ds = table_head;
325      table_head = NULL;
326      table_tail = NULL;
327      spin_unlock_irqrestore(&divert_lock, flags);
328      while (ds)
329       { ds1 = ds; 
330         ds = ds->next;
331         kfree(ds1);
332       } 
333      return(0); 
334    }
335
336   spin_lock_irqsave(&divert_lock, flags);
337   ds = table_head;
338
339   while ((ds) && (idx > 0))
340    { idx--; 
341      ds = ds->next;  
342    }
343
344   if (!ds) 
345    {
346      spin_unlock_irqrestore(&divert_lock, flags);
347      return(-EINVAL);
348    }  
349
350   if (ds->next) 
351     ds->next->prev = ds->prev; /* backward chain */
352    else
353      table_tail = ds->prev; /* end of chain */
354
355   if (ds->prev)
356     ds->prev->next = ds->next; /* forward chain */
357    else
358      table_head = ds->next; /* start of chain */      
359   
360   spin_unlock_irqrestore(&divert_lock, flags);
361   kfree(ds);
362   return(0);
363 } /* deleterule */
364
365 /*******************************************/
366 /* get a pointer to a specific rule number */
367 /*******************************************/
368 divert_rule *getruleptr(int idx)
369 { struct deflect_struc *ds = table_head;
370   
371   if (idx < 0) return(NULL);
372   while ((ds) && (idx >= 0))
373    { if (!(idx--)) 
374       { return(&ds->rule);
375         break;
376       }
377      ds = ds->next;  
378    }
379   return(NULL);
380 } /* getruleptr */
381
382 /*************************************************/
383 /* called from common module on an incoming call */
384 /*************************************************/
385 static int isdn_divert_icall(isdn_ctrl *ic)
386 { int retval = 0;
387   unsigned long flags;
388   struct call_struc *cs = NULL; 
389   struct deflect_struc *dv;
390   char *p,*p1;
391   u_char accept;
392
393   /* first check the internal deflection table */
394   for (dv = table_head; dv ; dv = dv->next )
395    { /* scan table */
396      if (((dv->rule.callopt == 1) && (ic->command == ISDN_STAT_ICALLW)) ||
397          ((dv->rule.callopt == 2) && (ic->command == ISDN_STAT_ICALL)))
398        continue; /* call option check */  
399      if (!(dv->rule.drvid & (1L << ic->driver))) 
400        continue; /* driver not matching */ 
401      if ((dv->rule.si1) && (dv->rule.si1 != ic->parm.setup.si1)) 
402        continue; /* si1 not matching */
403      if ((dv->rule.si2) && (dv->rule.si2 != ic->parm.setup.si2)) 
404        continue; /* si2 not matching */
405
406      p = dv->rule.my_msn;
407      p1 = ic->parm.setup.eazmsn;
408      accept = 0;
409      while (*p)
410       { /* complete compare */
411         if (*p == '-')
412           { accept = 1; /* call accepted */
413             break;
414           }
415         if (*p++ != *p1++) 
416           break; /* not accepted */
417         if ((!*p) && (!*p1))
418           accept = 1;
419       } /* complete compare */
420      if (!accept) continue; /* not accepted */
421  
422      if ((strcmp(dv->rule.caller,"0")) || (ic->parm.setup.phone[0]))
423       { p = dv->rule.caller;
424         p1 = ic->parm.setup.phone;
425         accept = 0;
426         while (*p)
427          { /* complete compare */
428            if (*p == '-')
429             { accept = 1; /* call accepted */
430               break;
431             }
432            if (*p++ != *p1++) 
433              break; /* not accepted */
434            if ((!*p) && (!*p1))
435              accept = 1;
436          } /* complete compare */
437         if (!accept) continue; /* not accepted */
438       }  
439
440      switch (dv->rule.action)
441        { case DEFLECT_IGNORE:
442            return(0);
443            break;
444
445          case DEFLECT_ALERT:
446          case DEFLECT_PROCEED:
447          case DEFLECT_REPORT:
448          case DEFLECT_REJECT:
449            if (dv->rule.action == DEFLECT_PROCEED)
450             if ((!if_used) || ((!extern_wait_max) && (!dv->rule.waittime))) 
451               return(0); /* no external deflection needed */  
452            if (!(cs = (struct call_struc *) kmalloc(sizeof(struct call_struc), GFP_ATOMIC))) 
453              return(0); /* no memory */
454            init_timer(&cs->timer);
455            cs->info[0] = '\0';
456            cs->timer.function = deflect_timer_expire;
457            cs->timer.data = (ulong) cs; /* pointer to own structure */
458            
459            cs->ics = *ic; /* copy incoming data */
460            if (!cs->ics.parm.setup.phone[0]) strcpy(cs->ics.parm.setup.phone,"0");
461            if (!cs->ics.parm.setup.eazmsn[0]) strcpy(cs->ics.parm.setup.eazmsn,"0");
462            cs->ics.parm.setup.screen = dv->rule.screen;  
463            if (dv->rule.waittime) 
464              cs->timer.expires = jiffies + (HZ * dv->rule.waittime);
465            else
466             if (dv->rule.action == DEFLECT_PROCEED)
467               cs->timer.expires = jiffies + (HZ * extern_wait_max); 
468             else  
469               cs->timer.expires = 0;
470            cs->akt_state = dv->rule.action;                
471            spin_lock_irqsave(&divert_lock, flags);
472            cs->divert_id = next_id++; /* new sequence number */
473            spin_unlock_irqrestore(&divert_lock, flags);
474            cs->prev = NULL;
475            if (cs->akt_state == DEFLECT_ALERT)
476              { strcpy(cs->deflect_dest,dv->rule.to_nr);
477                if (!cs->timer.expires)
478                  { strcpy(ic->parm.setup.eazmsn,"Testtext direct");
479                    ic->parm.setup.screen = dv->rule.screen;
480                    strcpy(ic->parm.setup.phone,dv->rule.to_nr);
481                    cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */
482                    cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);
483                    retval = 5; 
484                  }
485                else
486                  retval = 1; /* alerting */                 
487              }
488            else
489              { cs->deflect_dest[0] = '\0';
490                retval = 4; /* only proceed */
491              }  
492            sprintf(cs->info,"%d 0x%lx %s %s %s %s 0x%x 0x%x %d %d %s\n",
493                    cs->akt_state,
494                    cs->divert_id,
495                    divert_if.drv_to_name(cs->ics.driver),
496                    (ic->command == ISDN_STAT_ICALLW) ? "1":"0", 
497                    cs->ics.parm.setup.phone, 
498                    cs->ics.parm.setup.eazmsn,
499                    cs->ics.parm.setup.si1,
500                    cs->ics.parm.setup.si2,
501                    cs->ics.parm.setup.screen,
502                    dv->rule.waittime,
503                    cs->deflect_dest);
504            if ((dv->rule.action == DEFLECT_REPORT) ||
505                (dv->rule.action == DEFLECT_REJECT))
506             { put_info_buffer(cs->info);
507               kfree(cs); /* remove */
508               return((dv->rule.action == DEFLECT_REPORT) ? 0:2); /* nothing to do */ 
509             }              
510            break;
511   
512          default:
513            return(0); /* ignore call */
514            break;
515        } /* switch action */    
516      break; 
517    } /* scan_table */
518
519   if (cs) 
520    { cs->prev = NULL;
521      spin_lock_irqsave(&divert_lock, flags);
522      cs->next = divert_head;
523      divert_head = cs; 
524      if (cs->timer.expires) add_timer(&cs->timer);
525      spin_unlock_irqrestore(&divert_lock, flags);
526
527      put_info_buffer(cs->info); 
528      return(retval);
529    }
530   else
531      return(0);
532 } /* isdn_divert_icall */
533
534
535 void deleteprocs(void)
536 { struct call_struc *cs, *cs1; 
537   unsigned long flags;
538
539   spin_lock_irqsave(&divert_lock, flags);
540   cs = divert_head;
541   divert_head = NULL;
542   while (cs)
543    { del_timer(&cs->timer);
544      cs1 = cs;
545      cs = cs->next;
546      kfree(cs1);
547    } 
548   spin_unlock_irqrestore(&divert_lock, flags);
549 } /* deleteprocs */
550
551 /****************************************************/
552 /* put a address including address type into buffer */
553 /****************************************************/
554 static int put_address(char *st, u_char *p, int len)
555 { u_char retval = 0;
556   u_char adr_typ = 0; /* network standard */
557
558   if (len < 2) return(retval);
559   if (*p == 0xA1)
560    { retval = *(++p) + 2; /* total length */
561      if (retval > len) return(0); /* too short */
562      len = retval - 2; /* remaining length */
563      if (len < 3) return(0);
564      if ((*(++p) != 0x0A) || (*(++p) != 1)) return(0);
565      adr_typ = *(++p);
566      len -= 3;
567      p++;
568      if (len < 2) return(0);
569      if (*p++ != 0x12) return(0);
570      if (*p > len) return(0); /* check number length */
571      len = *p++;
572    }   
573   else
574    if (*p == 0x80)
575     { retval = *(++p) + 2; /* total length */
576       if (retval > len) return(0);
577       len = retval - 2;
578       p++;
579     }
580    else  
581     return(0); /* invalid address information */
582
583   sprintf(st,"%d ",adr_typ);
584   st += strlen(st);
585   if (!len) 
586     *st++ = '-';
587   else
588    while (len--)
589      *st++ = *p++;
590   *st = '\0';
591   return(retval);
592 } /* put_address */
593
594 /*************************************/
595 /* report a succesfull interrogation */
596 /*************************************/
597 static int interrogate_success(isdn_ctrl *ic, struct call_struc *cs)
598 { char *src = ic->parm.dss1_io.data;
599   int restlen = ic->parm.dss1_io.datalen;
600   int cnt = 1;
601   u_char n,n1;
602   char st[90], *p, *stp;
603
604   if (restlen < 2) return(-100); /* frame too short */
605   if (*src++ != 0x30) return(-101);
606   if ((n = *src++) > 0x81) return(-102); /* invalid length field */
607   restlen -= 2; /* remaining bytes */
608   if (n == 0x80)
609    { if (restlen < 2) return(-103);
610      if ((*(src+restlen-1)) || (*(src+restlen-2))) return(-104);
611      restlen -= 2;
612    }
613   else
614    if ( n == 0x81)
615     { n = *src++;
616       restlen--;
617       if (n > restlen) return(-105);
618       restlen = n;
619     }
620    else
621     if (n > restlen) return(-106);
622      else 
623       restlen = n; /* standard format */   
624   if (restlen < 3) return(-107); /* no procedure */
625   if ((*src++ != 2) || (*src++ != 1) || (*src++ != 0x0B)) return(-108);
626   restlen -= 3; 
627   if (restlen < 2) return(-109); /* list missing */
628   if (*src == 0x31)
629    { src++; 
630      if ((n = *src++) > 0x81) return(-110); /* invalid length field */
631      restlen -= 2; /* remaining bytes */
632      if (n == 0x80)
633       { if (restlen < 2) return(-111);
634         if ((*(src+restlen-1)) || (*(src+restlen-2))) return(-112);
635         restlen -= 2;
636       }
637      else
638       if ( n == 0x81)
639        { n = *src++;
640          restlen--;
641          if (n > restlen) return(-113);
642          restlen = n;
643        }
644       else
645        if (n > restlen) return(-114);
646         else 
647          restlen = n; /* standard format */   
648    } /* result list header */ 
649
650   while (restlen >= 2)
651    { stp = st;
652      sprintf(stp,"%d 0x%lx %d %s ",DIVERT_REPORT, ic->parm.dss1_io.ll_id,
653                  cnt++,divert_if.drv_to_name(ic->driver));
654      stp += strlen(stp);
655      if (*src++ != 0x30) return(-115); /* invalid enum */
656      n = *src++;
657      restlen -= 2;
658      if (n > restlen) return(-116); /* enum length wrong */
659      restlen -= n;
660      p = src; /* one entry */
661      src += n;
662      if (!(n1 = put_address(stp,p,n & 0xFF))) continue;
663      stp += strlen(stp);
664      p += n1;
665      n -= n1;
666      if (n < 6) continue; /* no service and proc */
667      if ((*p++ != 0x0A) || (*p++ != 1)) continue;
668      sprintf(stp," 0x%02x ",(*p++) & 0xFF);
669      stp += strlen(stp);
670      if ((*p++ != 0x0A) || (*p++ != 1)) continue;
671      sprintf(stp,"%d ",(*p++) & 0xFF);
672      stp += strlen(stp);
673      n -= 6;
674      if (n > 2)
675       { if (*p++ != 0x30) continue;
676         if (*p > (n-2)) continue;
677         n = *p++;
678         if (!(n1 = put_address(stp,p,n & 0xFF))) continue;
679         stp += strlen(stp);
680       }
681      sprintf(stp,"\n");
682      put_info_buffer(st);
683    } /* while restlen */
684   if (restlen) return(-117);
685   return(0);   
686 } /* interrogate_success */
687
688 /*********************************************/
689 /* callback for protocol specific extensions */
690 /*********************************************/
691 static int prot_stat_callback(isdn_ctrl *ic)
692 { struct call_struc *cs, *cs1;
693   int i;
694   unsigned long flags;
695
696   cs = divert_head; /* start of list */
697   cs1 = NULL;
698   while (cs)
699    { if (ic->driver == cs->ics.driver) 
700       { switch (cs->ics.arg)
701          { case DSS1_CMD_INVOKE:
702              if ((cs->ics.parm.dss1_io.ll_id == ic->parm.dss1_io.ll_id) &&
703                  (cs->ics.parm.dss1_io.hl_id == ic->parm.dss1_io.hl_id))
704               { switch (ic->arg)
705                 {  case DSS1_STAT_INVOKE_ERR:
706                      sprintf(cs->info,"128 0x%lx 0x%x\n", 
707                              ic->parm.dss1_io.ll_id,
708                              ic->parm.dss1_io.timeout);
709                      put_info_buffer(cs->info);
710                    break;
711                    
712                    case DSS1_STAT_INVOKE_RES:
713                      switch (cs->ics.parm.dss1_io.proc)
714                       {  case  7:
715                          case  8:
716                             put_info_buffer(cs->info); 
717                            break;
718                        
719                          case  11:
720                            i = interrogate_success(ic,cs);
721                            if (i)
722                              sprintf(cs->info,"%d 0x%lx %d\n",DIVERT_REPORT, 
723                                      ic->parm.dss1_io.ll_id,i);
724                            put_info_buffer(cs->info); 
725                            break;
726                        
727                          default: 
728                            printk(KERN_WARNING "dss1_divert: unknown proc %d\n",cs->ics.parm.dss1_io.proc);
729                            break;
730                       } 
731
732
733                    break;
734  
735                    default:
736                      printk(KERN_WARNING "dss1_divert unknown invoke answer %lx\n",ic->arg);
737                    break;  
738                  } 
739                 cs1 = cs; /* remember structure */
740                 cs = NULL; 
741                 continue; /* abort search */
742               } /* id found */ 
743            break;
744    
745            case DSS1_CMD_INVOKE_ABORT:
746              printk(KERN_WARNING "dss1_divert unhandled invoke abort\n"); 
747            break;   
748          
749            default:
750              printk(KERN_WARNING "dss1_divert unknown cmd 0x%lx\n",cs->ics.arg); 
751            break; 
752          } /* switch ics.arg */ 
753         cs = cs->next; 
754       } /* driver ok */
755    }  
756    
757   if (!cs1) 
758    { printk(KERN_WARNING "dss1_divert unhandled process\n");
759      return(0);
760    }  
761
762   if (cs1->ics.driver == -1)
763    {
764      spin_lock_irqsave(&divert_lock, flags);
765      del_timer(&cs1->timer);
766      if (cs1->prev) 
767        cs1->prev->next = cs1->next; /* forward link */
768      else
769        divert_head = cs1->next;
770      if (cs1->next)
771        cs1->next->prev = cs1->prev; /* back link */           
772      spin_unlock_irqrestore(&divert_lock, flags);
773      kfree(cs1);
774    } 
775
776   return(0);
777 } /* prot_stat_callback */
778
779
780 /***************************/
781 /* status callback from HL */
782 /***************************/
783 static int isdn_divert_stat_callback(isdn_ctrl *ic)
784 { struct call_struc *cs, *cs1;
785   unsigned long flags;
786   int retval;
787
788   retval = -1;
789   cs = divert_head; /* start of list */
790      while (cs)
791       { if ((ic->driver == cs->ics.driver) && (ic->arg == cs->ics.arg))
792          { switch (ic->command)
793             { case ISDN_STAT_DHUP:
794                 sprintf(cs->info,"129 0x%lx\n",cs->divert_id);
795                 del_timer(&cs->timer);
796                 cs->ics.driver = -1;
797                 break;
798
799               case ISDN_STAT_CAUSE:
800                 sprintf(cs->info,"130 0x%lx %s\n",cs->divert_id,ic->parm.num);
801                 break;
802
803               case ISDN_STAT_REDIR:
804                 sprintf(cs->info,"131 0x%lx\n",cs->divert_id);
805                 del_timer(&cs->timer);
806                 cs->ics.driver = -1;
807                 break; 
808
809               default:
810                 sprintf(cs->info,"999 0x%lx 0x%x\n",cs->divert_id,(int)(ic->command));
811                 break; 
812             }
813           put_info_buffer(cs->info);
814           retval = 0; 
815          }
816         cs1 = cs; 
817         cs = cs->next;
818         if (cs1->ics.driver == -1)
819           { 
820             spin_lock_irqsave(&divert_lock, flags);
821             if (cs1->prev) 
822               cs1->prev->next = cs1->next; /* forward link */
823             else
824               divert_head = cs1->next;
825             if (cs1->next)
826               cs1->next->prev = cs1->prev; /* back link */           
827             spin_unlock_irqrestore(&divert_lock, flags);
828             kfree(cs1);
829           } 
830       }  
831   return(retval); /* not found */
832 } /* isdn_divert_stat_callback */ 
833
834
835 /********************/
836 /* callback from ll */
837 /********************/ 
838 int ll_callback(isdn_ctrl *ic)
839 {
840   switch (ic->command)
841    { case ISDN_STAT_ICALL:
842      case ISDN_STAT_ICALLW:
843        return(isdn_divert_icall(ic));
844      break;
845
846      case ISDN_STAT_PROT:
847        if ((ic->arg & 0xFF) == ISDN_PTYPE_EURO)
848         { if (ic->arg != DSS1_STAT_INVOKE_BRD)
849             return(prot_stat_callback(ic));
850           else
851             return(0); /* DSS1 invoke broadcast */
852         }
853        else
854          return(-1); /* protocol not euro */    
855
856      default:
857        return(isdn_divert_stat_callback(ic));
858    }
859 } /* ll_callback */
860