added mtd driver
[linux-2.4.git] / drivers / isdn / hisax / l3dss1.c
1 /* $Id: l3dss1.c,v 1.1.4.1 2001/11/20 14:19:36 kai Exp $
2  *
3  * EURO/DSS1 D-channel protocol
4  *
5  * German 1TR6 D-channel protocol
6  *
7  * Author       Karsten Keil
8  *              based on the teles driver from Jan den Ouden
9  * Copyright    by Karsten Keil      <keil@isdn4linux.de>
10  * 
11  * This software may be used and distributed according to the terms
12  * of the GNU General Public License, incorporated herein by reference.
13  *
14  * For changes and modifications please read
15  * ../../../Documentation/isdn/HiSax.cert
16  *
17  * Thanks to    Jan den Ouden
18  *              Fritz Elfert
19  *
20  */
21
22 #define __NO_VERSION__
23 #include "hisax.h"
24 #include "isdnl3.h"
25 #include "l3dss1.h"
26 #include <linux/ctype.h>
27 #include <linux/config.h>
28
29 extern char *HiSax_getrev(const char *revision);
30 const char *dss1_revision = "$Revision: 1.1.4.1 $";
31
32 #define EXT_BEARER_CAPS 1
33
34 #define MsgHead(ptr, cref, mty) \
35         *ptr++ = 0x8; \
36         if (cref == -1) { \
37                 *ptr++ = 0x0; \
38         } else { \
39                 *ptr++ = 0x1; \
40                 *ptr++ = cref^0x80; \
41         } \
42         *ptr++ = mty
43
44
45 /**********************************************/
46 /* get a new invoke id for remote operations. */
47 /* Only a return value != 0 is valid          */
48 /**********************************************/
49 static unsigned char new_invoke_id(struct PStack *p)
50 {
51         unsigned char retval;
52         unsigned long flags;
53         int i;
54   
55         i = 32; /* maximum search depth */
56
57         save_flags(flags);
58         cli();
59
60         retval = p->prot.dss1.last_invoke_id + 1; /* try new id */
61         while ((i) && (p->prot.dss1.invoke_used[retval >> 3] == 0xFF)) {
62                 p->prot.dss1.last_invoke_id = (retval & 0xF8) + 8;
63                 i--;
64         }  
65         if (i) {
66                 while (p->prot.dss1.invoke_used[retval >> 3] & (1 << (retval & 7)))
67                 retval++; 
68         } else
69                 retval = 0;
70         p->prot.dss1.last_invoke_id = retval;
71         p->prot.dss1.invoke_used[retval >> 3] |= (1 << (retval & 7));
72         restore_flags(flags);
73
74         return(retval);  
75 } /* new_invoke_id */
76
77 /*************************/
78 /* free a used invoke id */
79 /*************************/
80 static void free_invoke_id(struct PStack *p, unsigned char id)
81 { unsigned long flags;
82
83   if (!id) return; /* 0 = invalid value */
84
85   save_flags(flags);
86   cli();
87   p->prot.dss1.invoke_used[id >> 3] &= ~(1 << (id & 7));
88   restore_flags(flags);
89 } /* free_invoke_id */  
90
91
92 /**********************************************************/
93 /* create a new l3 process and fill in dss1 specific data */
94 /**********************************************************/
95 static struct l3_process
96 *dss1_new_l3_process(struct PStack *st, int cr)
97 {  struct l3_process *proc;
98
99    if (!(proc = new_l3_process(st, cr))) 
100      return(NULL);
101
102    proc->prot.dss1.invoke_id = 0;
103    proc->prot.dss1.remote_operation = 0;
104    proc->prot.dss1.uus1_data[0] = '\0';
105    
106    return(proc);
107 } /* dss1_new_l3_process */
108
109 /************************************************/
110 /* free a l3 process and all dss1 specific data */
111 /************************************************/ 
112 static void
113 dss1_release_l3_process(struct l3_process *p)
114 {
115    free_invoke_id(p->st,p->prot.dss1.invoke_id);
116    release_l3_process(p);
117 } /* dss1_release_l3_process */
118  
119 /********************************************************/
120 /* search a process with invoke id id and dummy callref */
121 /********************************************************/
122 static struct l3_process *
123 l3dss1_search_dummy_proc(struct PStack *st, int id)
124 { struct l3_process *pc = st->l3.proc; /* start of processes */
125
126   if (!id) return(NULL);
127
128   while (pc)
129    { if ((pc->callref == -1) && (pc->prot.dss1.invoke_id == id))
130        return(pc);
131      pc = pc->next;
132    } 
133   return(NULL);
134 } /* l3dss1_search_dummy_proc */
135
136 /*******************************************************************/
137 /* called when a facility message with a dummy callref is received */
138 /* and a return result is delivered. id specifies the invoke id.   */
139 /*******************************************************************/ 
140 static void 
141 l3dss1_dummy_return_result(struct PStack *st, int id, u_char *p, u_char nlen)
142 { isdn_ctrl ic;
143   struct IsdnCardState *cs;
144   struct l3_process *pc = NULL; 
145
146   if ((pc = l3dss1_search_dummy_proc(st, id)))
147    { L3DelTimer(&pc->timer); /* remove timer */
148
149      cs = pc->st->l1.hardware;
150      ic.driver = cs->myid;
151      ic.command = ISDN_STAT_PROT;
152      ic.arg = DSS1_STAT_INVOKE_RES;
153      ic.parm.dss1_io.hl_id = pc->prot.dss1.invoke_id;
154      ic.parm.dss1_io.ll_id = pc->prot.dss1.ll_id;
155      ic.parm.dss1_io.proc = pc->prot.dss1.proc;
156      ic.parm.dss1_io.timeout= 0;
157      ic.parm.dss1_io.datalen = nlen;
158      ic.parm.dss1_io.data = p;
159      free_invoke_id(pc->st, pc->prot.dss1.invoke_id);
160      pc->prot.dss1.invoke_id = 0; /* reset id */
161
162      cs->iif.statcallb(&ic);
163      dss1_release_l3_process(pc); 
164    }
165   else
166    l3_debug(st, "dummy return result id=0x%x result len=%d",id,nlen);
167 } /* l3dss1_dummy_return_result */
168
169 /*******************************************************************/
170 /* called when a facility message with a dummy callref is received */
171 /* and a return error is delivered. id specifies the invoke id.    */
172 /*******************************************************************/ 
173 static void 
174 l3dss1_dummy_error_return(struct PStack *st, int id, ulong error)
175 { isdn_ctrl ic;
176   struct IsdnCardState *cs;
177   struct l3_process *pc = NULL; 
178
179   if ((pc = l3dss1_search_dummy_proc(st, id)))
180    { L3DelTimer(&pc->timer); /* remove timer */
181
182      cs = pc->st->l1.hardware;
183      ic.driver = cs->myid;
184      ic.command = ISDN_STAT_PROT;
185      ic.arg = DSS1_STAT_INVOKE_ERR;
186      ic.parm.dss1_io.hl_id = pc->prot.dss1.invoke_id;
187      ic.parm.dss1_io.ll_id = pc->prot.dss1.ll_id;
188      ic.parm.dss1_io.proc = pc->prot.dss1.proc;
189      ic.parm.dss1_io.timeout= error;
190      ic.parm.dss1_io.datalen = 0;
191      ic.parm.dss1_io.data = NULL;
192      free_invoke_id(pc->st, pc->prot.dss1.invoke_id);
193      pc->prot.dss1.invoke_id = 0; /* reset id */
194
195      cs->iif.statcallb(&ic);
196      dss1_release_l3_process(pc); 
197    }
198   else
199    l3_debug(st, "dummy return error id=0x%x error=0x%lx",id,error);
200 } /* l3dss1_error_return */
201
202 /*******************************************************************/
203 /* called when a facility message with a dummy callref is received */
204 /* and a invoke is delivered. id specifies the invoke id.          */
205 /*******************************************************************/ 
206 static void 
207 l3dss1_dummy_invoke(struct PStack *st, int cr, int id, 
208                     int ident, u_char *p, u_char nlen)
209 { isdn_ctrl ic;
210   struct IsdnCardState *cs;
211   
212   l3_debug(st, "dummy invoke %s id=0x%x ident=0x%x datalen=%d",
213                (cr == -1) ? "local" : "broadcast",id,ident,nlen);
214   if (cr >= -1) return; /* ignore local data */
215
216   cs = st->l1.hardware;
217   ic.driver = cs->myid;
218   ic.command = ISDN_STAT_PROT;
219   ic.arg = DSS1_STAT_INVOKE_BRD;
220   ic.parm.dss1_io.hl_id = id;
221   ic.parm.dss1_io.ll_id = 0;
222   ic.parm.dss1_io.proc = ident;
223   ic.parm.dss1_io.timeout= 0;
224   ic.parm.dss1_io.datalen = nlen;
225   ic.parm.dss1_io.data = p;
226
227   cs->iif.statcallb(&ic);
228 } /* l3dss1_dummy_invoke */
229
230 static void
231 l3dss1_parse_facility(struct PStack *st, struct l3_process *pc,
232                       int cr, u_char * p)
233 {
234         int qd_len = 0;
235         unsigned char nlen = 0, ilen, cp_tag;
236         int ident, id;
237         ulong err_ret;
238
239         if (pc) 
240                 st = pc->st; /* valid Stack */
241         else
242                 if ((!st) || (cr >= 0)) return; /* neither pc nor st specified */
243
244         p++;
245         qd_len = *p++;
246         if (qd_len == 0) {
247                 l3_debug(st, "qd_len == 0");
248                 return;
249         }
250         if ((*p & 0x1F) != 0x11) {      /* Service discriminator, supplementary service */
251                 l3_debug(st, "supplementary service != 0x11");
252                 return;
253         }
254         while (qd_len > 0 && !(*p & 0x80)) {    /* extension ? */
255                 p++;
256                 qd_len--;
257         }
258         if (qd_len < 2) {
259                 l3_debug(st, "qd_len < 2");
260                 return;
261         }
262         p++;
263         qd_len--;
264         if ((*p & 0xE0) != 0xA0) {      /* class and form */
265                 l3_debug(st, "class and form != 0xA0");
266                 return;
267         }
268        
269         cp_tag = *p & 0x1F; /* remember tag value */
270
271         p++;
272         qd_len--;
273         if (qd_len < 1) 
274           { l3_debug(st, "qd_len < 1");
275             return;
276           }
277         if (*p & 0x80) 
278           { /* length format indefinite or limited */
279             nlen = *p++ & 0x7F; /* number of len bytes or indefinite */
280             if ((qd_len-- < ((!nlen) ? 3 : (1 + nlen))) ||
281                 (nlen > 1))   
282              { l3_debug(st, "length format error or not implemented");
283                return;
284              }
285             if (nlen == 1)
286              { nlen = *p++; /* complete length */
287                qd_len--;
288              } 
289             else
290              { qd_len -= 2; /* trailing null bytes */
291                if ((*(p+qd_len)) || (*(p+qd_len+1)))
292                 { l3_debug(st,"length format indefinite error");
293                   return;
294                 }
295                nlen = qd_len;
296              }
297           }
298         else
299           { nlen = *p++;
300             qd_len--;
301           } 
302         if (qd_len < nlen) 
303           { l3_debug(st, "qd_len < nlen");
304             return;
305           }
306         qd_len -= nlen;
307
308         if (nlen < 2) 
309           { l3_debug(st, "nlen < 2");
310             return;
311           }
312         if (*p != 0x02) 
313           {  /* invoke identifier tag */
314              l3_debug(st, "invoke identifier tag !=0x02");
315              return;
316           }
317         p++;
318         nlen--;
319         if (*p & 0x80) 
320           { /* length format */
321             l3_debug(st, "invoke id length format 2");
322             return;
323           }
324         ilen = *p++;
325         nlen--;
326         if (ilen > nlen || ilen == 0) 
327           { l3_debug(st, "ilen > nlen || ilen == 0");
328             return;
329           }
330         nlen -= ilen;
331         id = 0;
332         while (ilen > 0) 
333           { id = (id << 8) | (*p++ & 0xFF);     /* invoke identifier */
334             ilen--;
335           }
336
337         switch (cp_tag) {       /* component tag */
338                 case 1: /* invoke */
339                                 if (nlen < 2) {
340                                         l3_debug(st, "nlen < 2 22");
341                                         return;
342                                 }
343                                 if (*p != 0x02) {       /* operation value */
344                                         l3_debug(st, "operation value !=0x02");
345                                         return;
346                                 }
347                                 p++;
348                                 nlen--;
349                                 ilen = *p++;
350                                 nlen--;
351                                 if (ilen > nlen || ilen == 0) {
352                                         l3_debug(st, "ilen > nlen || ilen == 0 22");
353                                         return;
354                                 }
355                                 nlen -= ilen;
356                                 ident = 0;
357                                 while (ilen > 0) {
358                                         ident = (ident << 8) | (*p++ & 0xFF);
359                                         ilen--;
360                                 }
361
362                                 if (!pc) 
363                                  { l3dss1_dummy_invoke(st, cr, id, ident, p, nlen);
364                                    return;
365                                  } 
366 #if HISAX_DE_AOC
367                         {
368
369 #define FOO1(s,a,b) \
370             while(nlen > 1) {           \
371                     int ilen = p[1];    \
372                     if(nlen < ilen+2) { \
373                             l3_debug(st, "FOO1  nlen < ilen+2"); \
374                             return;             \
375                     }                   \
376                     nlen -= ilen+2;             \
377                     if((*p & 0xFF) == (a)) {    \
378                             int nlen = ilen;    \
379                             p += 2;             \
380                             b;          \
381                     } else {            \
382                             p += ilen+2;        \
383                     }                   \
384             }
385
386                                 switch (ident) {
387                                         case 0x22:      /* during */
388                                                 FOO1("1A", 0x30, FOO1("1C", 0xA1, FOO1("1D", 0x30, FOO1("1E", 0x02, ( {
389                                                                ident = 0;
390                                                         nlen = (nlen)?nlen:0; /* Make gcc happy */
391                                                         while (ilen > 0) {
392                                                                                                                      ident = (ident << 8) | *p++;
393                                                                   ilen--;
394                                                                         }
395                                                                                                                      if (ident > pc->para.chargeinfo) {
396                                                                                                                      pc->para.chargeinfo = ident;
397                                                                                                                      st->l3.l3l4(st, CC_CHARGE | INDICATION, pc);
398                                                                         }
399                                                                                                                      if (st->l3.debug & L3_DEB_CHARGE) {
400                                                                                                                      if (*(p + 2) == 0) {
401                                                                                                                      l3_debug(st, "charging info during %d", pc->para.chargeinfo);
402                                                                         }
403                                                                    else {
404                                                                                                                      l3_debug(st, "charging info final %d", pc->para.chargeinfo);
405                                                                         }
406                                                                         }
407                                                                         }
408                                                                     )))))
409                                                         break;
410                                         case 0x24:      /* final */
411                                                 FOO1("2A", 0x30, FOO1("2B", 0x30, FOO1("2C", 0xA1, FOO1("2D", 0x30, FOO1("2E", 0x02, ( {
412                                                                ident = 0;
413                                                         nlen = (nlen)?nlen:0; /* Make gcc happy */
414                                                         while (ilen > 0) {
415                                                                                                                                       ident = (ident << 8) | *p++;
416                                                                   ilen--;
417                                                                         }
418                                                                                                                                       if (ident > pc->para.chargeinfo) {
419                                                                                                                                       pc->para.chargeinfo = ident;
420                                                                                                                                       st->l3.l3l4(st, CC_CHARGE | INDICATION, pc);
421                                                                         }
422                                                                                                                                       if (st->l3.debug & L3_DEB_CHARGE) {
423                                                                                                                                       l3_debug(st, "charging info final %d", pc->para.chargeinfo);
424                                                                         }
425                                                                         }
426                                                                    ))))))
427                                                         break;
428                                         default:
429                                                        l3_debug(st, "invoke break invalid ident %02x",ident);
430                                                 break;
431                                 }
432 #undef FOO1
433
434                         }
435 #else  /* not HISAX_DE_AOC */
436                         l3_debug(st, "invoke break");
437 #endif /* not HISAX_DE_AOC */
438                         break;
439                 case 2: /* return result */
440                          /* if no process available handle separately */ 
441                         if (!pc)
442                          { if (cr == -1) 
443                              l3dss1_dummy_return_result(st, id, p, nlen);
444                            return; 
445                          }   
446                         if ((pc->prot.dss1.invoke_id) && (pc->prot.dss1.invoke_id == id))
447                           { /* Diversion successful */
448                             free_invoke_id(st,pc->prot.dss1.invoke_id);
449                             pc->prot.dss1.remote_result = 0; /* success */     
450                             pc->prot.dss1.invoke_id = 0;
451                             pc->redir_result = pc->prot.dss1.remote_result; 
452                             st->l3.l3l4(st, CC_REDIR | INDICATION, pc);                                  } /* Diversion successful */
453                         else
454                           l3_debug(st,"return error unknown identifier");
455                         break;
456                 case 3: /* return error */
457                             err_ret = 0;
458                             if (nlen < 2) 
459                               { l3_debug(st, "return error nlen < 2");
460                                 return;
461                               }
462                             if (*p != 0x02) 
463                               { /* result tag */
464                                 l3_debug(st, "invoke error tag !=0x02");
465                                 return;
466                               }
467                             p++;
468                             nlen--;
469                             if (*p > 4) 
470                               { /* length format */
471                                 l3_debug(st, "invoke return errlen > 4 ");
472                                 return;
473                               }
474                             ilen = *p++;
475                             nlen--;
476                             if (ilen > nlen || ilen == 0) 
477                               { l3_debug(st, "error return ilen > nlen || ilen == 0");
478                                 return;
479                                }
480                             nlen -= ilen;
481                             while (ilen > 0) 
482                              { err_ret = (err_ret << 8) | (*p++ & 0xFF);        /* error value */
483                                ilen--;
484                              }
485                          /* if no process available handle separately */ 
486                         if (!pc)
487                          { if (cr == -1)
488                              l3dss1_dummy_error_return(st, id, err_ret);
489                            return; 
490                          }   
491                         if ((pc->prot.dss1.invoke_id) && (pc->prot.dss1.invoke_id == id))
492                           { /* Deflection error */
493                             free_invoke_id(st,pc->prot.dss1.invoke_id);
494                             pc->prot.dss1.remote_result = err_ret; /* result */
495                             pc->prot.dss1.invoke_id = 0; 
496                             pc->redir_result = pc->prot.dss1.remote_result; 
497                             st->l3.l3l4(st, CC_REDIR | INDICATION, pc);  
498                           } /* Deflection error */
499                         else
500                           l3_debug(st,"return result unknown identifier");
501                         break;
502                 default:
503                         l3_debug(st, "facility default break tag=0x%02x",cp_tag);
504                         break;
505         }
506 }
507
508 static void
509 l3dss1_message(struct l3_process *pc, u_char mt)
510 {
511         struct sk_buff *skb;
512         u_char *p;
513
514         if (!(skb = l3_alloc_skb(4)))
515                 return;
516         p = skb_put(skb, 4);
517         MsgHead(p, pc->callref, mt);
518         l3_msg(pc->st, DL_DATA | REQUEST, skb);
519 }
520
521 static void
522 l3dss1_message_cause(struct l3_process *pc, u_char mt, u_char cause)
523 {
524         struct sk_buff *skb;
525         u_char tmp[16];
526         u_char *p = tmp;
527         int l;
528
529         MsgHead(p, pc->callref, mt);
530         *p++ = IE_CAUSE;
531         *p++ = 0x2;
532         *p++ = 0x80;
533         *p++ = cause | 0x80;
534
535         l = p - tmp;
536         if (!(skb = l3_alloc_skb(l)))
537                 return;
538         memcpy(skb_put(skb, l), tmp, l);
539         l3_msg(pc->st, DL_DATA | REQUEST, skb);
540 }
541
542 static void
543 l3dss1_status_send(struct l3_process *pc, u_char pr, void *arg)
544 {
545         u_char tmp[16];
546         u_char *p = tmp;
547         int l;
548         struct sk_buff *skb;
549
550         MsgHead(p, pc->callref, MT_STATUS);
551
552         *p++ = IE_CAUSE;
553         *p++ = 0x2;
554         *p++ = 0x80;
555         *p++ = pc->para.cause | 0x80;
556
557         *p++ = IE_CALL_STATE;
558         *p++ = 0x1;
559         *p++ = pc->state & 0x3f;
560
561         l = p - tmp;
562         if (!(skb = l3_alloc_skb(l)))
563                 return;
564         memcpy(skb_put(skb, l), tmp, l);
565         l3_msg(pc->st, DL_DATA | REQUEST, skb);
566 }
567
568 static void
569 l3dss1_msg_without_setup(struct l3_process *pc, u_char pr, void *arg)
570 {
571         /* This routine is called if here was no SETUP made (checks in dss1up and in
572          * l3dss1_setup) and a RELEASE_COMPLETE have to be sent with an error code
573          * MT_STATUS_ENQUIRE in the NULL state is handled too
574          */
575         u_char tmp[16];
576         u_char *p = tmp;
577         int l;
578         struct sk_buff *skb;
579
580         switch (pc->para.cause) {
581                 case 81:        /* invalid callreference */
582                 case 88:        /* incomp destination */
583                 case 96:        /* mandory IE missing */
584                 case 100:       /* invalid IE contents */
585                 case 101:       /* incompatible Callstate */
586                         MsgHead(p, pc->callref, MT_RELEASE_COMPLETE);
587                         *p++ = IE_CAUSE;
588                         *p++ = 0x2;
589                         *p++ = 0x80;
590                         *p++ = pc->para.cause | 0x80;
591                         break;
592                 default:
593                         printk(KERN_ERR "HiSax l3dss1_msg_without_setup wrong cause %d\n",
594                                 pc->para.cause);
595                         return;
596         }
597         l = p - tmp;
598         if (!(skb = l3_alloc_skb(l)))
599                 return;
600         memcpy(skb_put(skb, l), tmp, l);
601         l3_msg(pc->st, DL_DATA | REQUEST, skb);
602         dss1_release_l3_process(pc);
603 }
604
605 static int ie_ALERTING[] = {IE_BEARER, IE_CHANNEL_ID | IE_MANDATORY_1,
606                 IE_FACILITY, IE_PROGRESS, IE_DISPLAY, IE_SIGNAL, IE_HLC,
607                 IE_USER_USER, -1};
608 static int ie_CALL_PROCEEDING[] = {IE_BEARER, IE_CHANNEL_ID | IE_MANDATORY_1,
609                 IE_FACILITY, IE_PROGRESS, IE_DISPLAY, IE_HLC, -1};
610 static int ie_CONNECT[] = {IE_BEARER, IE_CHANNEL_ID | IE_MANDATORY_1, 
611                 IE_FACILITY, IE_PROGRESS, IE_DISPLAY, IE_DATE, IE_SIGNAL,
612                 IE_CONNECT_PN, IE_CONNECT_SUB, IE_LLC, IE_HLC, IE_USER_USER, -1};
613 static int ie_CONNECT_ACKNOWLEDGE[] = {IE_CHANNEL_ID, IE_DISPLAY, IE_SIGNAL, -1};
614 static int ie_DISCONNECT[] = {IE_CAUSE | IE_MANDATORY, IE_FACILITY,
615                 IE_PROGRESS, IE_DISPLAY, IE_SIGNAL, IE_USER_USER, -1};
616 static int ie_INFORMATION[] = {IE_COMPLETE, IE_DISPLAY, IE_KEYPAD, IE_SIGNAL,
617                 IE_CALLED_PN, -1};
618 static int ie_NOTIFY[] = {IE_BEARER, IE_NOTIFY | IE_MANDATORY, IE_DISPLAY, -1};
619 static int ie_PROGRESS[] = {IE_BEARER, IE_CAUSE, IE_FACILITY, IE_PROGRESS |
620                 IE_MANDATORY, IE_DISPLAY, IE_HLC, IE_USER_USER, -1};
621 static int ie_RELEASE[] = {IE_CAUSE | IE_MANDATORY_1, IE_FACILITY, IE_DISPLAY,
622                 IE_SIGNAL, IE_USER_USER, -1};
623 /* a RELEASE_COMPLETE with errors don't require special actions 
624 static int ie_RELEASE_COMPLETE[] = {IE_CAUSE | IE_MANDATORY_1, IE_DISPLAY, IE_SIGNAL, IE_USER_USER, -1};
625 */
626 static int ie_RESUME_ACKNOWLEDGE[] = {IE_CHANNEL_ID| IE_MANDATORY, IE_FACILITY,
627                 IE_DISPLAY, -1};
628 static int ie_RESUME_REJECT[] = {IE_CAUSE | IE_MANDATORY, IE_DISPLAY, -1};
629 static int ie_SETUP[] = {IE_COMPLETE, IE_BEARER  | IE_MANDATORY,
630                 IE_CHANNEL_ID| IE_MANDATORY, IE_FACILITY, IE_PROGRESS,
631                 IE_NET_FAC, IE_DISPLAY, IE_KEYPAD, IE_SIGNAL, IE_CALLING_PN,
632                 IE_CALLING_SUB, IE_CALLED_PN, IE_CALLED_SUB, IE_REDIR_NR,
633                 IE_LLC, IE_HLC, IE_USER_USER, -1};
634 static int ie_SETUP_ACKNOWLEDGE[] = {IE_CHANNEL_ID | IE_MANDATORY, IE_FACILITY,
635                 IE_PROGRESS, IE_DISPLAY, IE_SIGNAL, -1};
636 static int ie_STATUS[] = {IE_CAUSE | IE_MANDATORY, IE_CALL_STATE |
637                 IE_MANDATORY, IE_DISPLAY, -1};
638 static int ie_STATUS_ENQUIRY[] = {IE_DISPLAY, -1};
639 static int ie_SUSPEND_ACKNOWLEDGE[] = {IE_DISPLAY, IE_FACILITY, -1};
640 static int ie_SUSPEND_REJECT[] = {IE_CAUSE | IE_MANDATORY, IE_DISPLAY, -1};
641 /* not used 
642  * static int ie_CONGESTION_CONTROL[] = {IE_CONGESTION | IE_MANDATORY,
643  *              IE_CAUSE | IE_MANDATORY, IE_DISPLAY, -1};
644  * static int ie_USER_INFORMATION[] = {IE_MORE_DATA, IE_USER_USER | IE_MANDATORY, -1};
645  * static int ie_RESTART[] = {IE_CHANNEL_ID, IE_DISPLAY, IE_RESTART_IND |
646  *              IE_MANDATORY, -1};
647  */
648 static int ie_FACILITY[] = {IE_FACILITY | IE_MANDATORY, IE_DISPLAY, -1};
649 static int comp_required[] = {1,2,3,5,6,7,9,10,11,14,15,-1};
650 static int l3_valid_states[] = {0,1,2,3,4,6,7,8,9,10,11,12,15,17,19,25,-1};
651
652 struct ie_len {
653         int ie;
654         int len;
655 };
656
657 static
658 struct ie_len max_ie_len[] = {
659         {IE_SEGMENT, 4},
660         {IE_BEARER, 12},
661         {IE_CAUSE, 32},
662         {IE_CALL_ID, 10},
663         {IE_CALL_STATE, 3},
664         {IE_CHANNEL_ID, 34},
665         {IE_FACILITY, 255},
666         {IE_PROGRESS, 4},
667         {IE_NET_FAC, 255},
668         {IE_NOTIFY, 3},
669         {IE_DISPLAY, 82},
670         {IE_DATE, 8},
671         {IE_KEYPAD, 34},
672         {IE_SIGNAL, 3},
673         {IE_INFORATE, 6},
674         {IE_E2E_TDELAY, 11},
675         {IE_TDELAY_SEL, 5},
676         {IE_PACK_BINPARA, 3},
677         {IE_PACK_WINSIZE, 4},
678         {IE_PACK_SIZE, 4},
679         {IE_CUG, 7},
680         {IE_REV_CHARGE, 3},
681         {IE_CALLING_PN, 24},
682         {IE_CALLING_SUB, 23},
683         {IE_CALLED_PN, 24},
684         {IE_CALLED_SUB, 23},
685         {IE_REDIR_NR, 255},
686         {IE_TRANS_SEL, 255},
687         {IE_RESTART_IND, 3},
688         {IE_LLC, 18},
689         {IE_HLC, 5},
690         {IE_USER_USER, 131},
691         {-1,0},
692 };
693
694 static int
695 getmax_ie_len(u_char ie) {
696         int i = 0;
697         while (max_ie_len[i].ie != -1) {
698                 if (max_ie_len[i].ie == ie)
699                         return(max_ie_len[i].len);
700                 i++;
701         }
702         return(255);
703 }
704
705 static int
706 ie_in_set(struct l3_process *pc, u_char ie, int *checklist) {
707         int ret = 1;
708
709         while (*checklist != -1) {
710                 if ((*checklist & 0xff) == ie) {
711                         if (ie & 0x80)
712                                 return(-ret);
713                         else
714                                 return(ret);
715                 }
716                 ret++;
717                 checklist++;
718         }
719         return(0);
720 }
721
722 static int
723 check_infoelements(struct l3_process *pc, struct sk_buff *skb, int *checklist)
724 {
725         int *cl = checklist;
726         u_char mt;
727         u_char *p, ie;
728         int l, newpos, oldpos;
729         int err_seq = 0, err_len = 0, err_compr = 0, err_ureg = 0;
730         u_char codeset = 0;
731         u_char old_codeset = 0;
732         u_char codelock = 1;
733         
734         p = skb->data;
735         /* skip cr */
736         p++;
737         l = (*p++) & 0xf;
738         p += l;
739         mt = *p++;
740         oldpos = 0;
741         while ((p - skb->data) < skb->len) {
742                 if ((*p & 0xf0) == 0x90) { /* shift codeset */
743                         old_codeset = codeset;
744                         codeset = *p & 7;
745                         if (*p & 0x08)
746                                 codelock = 0;
747                         else
748                                 codelock = 1;
749                         if (pc->debug & L3_DEB_CHECK)
750                                 l3_debug(pc->st, "check IE shift%scodeset %d->%d",
751                                         codelock ? " locking ": " ", old_codeset, codeset);
752                         p++;
753                         continue;
754                 }
755                 if (!codeset) { /* only codeset 0 */
756                         if ((newpos = ie_in_set(pc, *p, cl))) {
757                                 if (newpos > 0) {
758                                         if (newpos < oldpos)
759                                                 err_seq++;
760                                         else
761                                                 oldpos = newpos;
762                                 }
763                         } else {
764                                 if (ie_in_set(pc, *p, comp_required))
765                                         err_compr++;
766                                 else
767                                         err_ureg++;
768                         }
769                 }
770                 ie = *p++;
771                 if (ie & 0x80) {
772                         l = 1;
773                 } else {
774                         l = *p++;
775                         p += l;
776                         l += 2;
777                 }
778                 if (!codeset && (l > getmax_ie_len(ie)))
779                         err_len++;
780                 if (!codelock) {
781                         if (pc->debug & L3_DEB_CHECK)
782                                 l3_debug(pc->st, "check IE shift back codeset %d->%d",
783                                         codeset, old_codeset);
784                         codeset = old_codeset;
785                         codelock = 1;
786                 }
787         }
788         if (err_compr | err_ureg | err_len | err_seq) {
789                 if (pc->debug & L3_DEB_CHECK)
790                         l3_debug(pc->st, "check IE MT(%x) %d/%d/%d/%d",
791                                 mt, err_compr, err_ureg, err_len, err_seq);
792                 if (err_compr)
793                         return(ERR_IE_COMPREHENSION);
794                 if (err_ureg)
795                         return(ERR_IE_UNRECOGNIZED);
796                 if (err_len)
797                         return(ERR_IE_LENGTH);
798                 if (err_seq)
799                         return(ERR_IE_SEQUENCE);
800         } 
801         return(0);
802 }
803
804 /* verify if a message type exists and contain no IE error */
805 static int
806 l3dss1_check_messagetype_validity(struct l3_process *pc, int mt, void *arg)
807 {
808         switch (mt) {
809                 case MT_ALERTING:
810                 case MT_CALL_PROCEEDING:
811                 case MT_CONNECT:
812                 case MT_CONNECT_ACKNOWLEDGE:
813                 case MT_DISCONNECT:
814                 case MT_INFORMATION:
815                 case MT_FACILITY:
816                 case MT_NOTIFY:
817                 case MT_PROGRESS:
818                 case MT_RELEASE:
819                 case MT_RELEASE_COMPLETE:
820                 case MT_SETUP:
821                 case MT_SETUP_ACKNOWLEDGE:
822                 case MT_RESUME_ACKNOWLEDGE:
823                 case MT_RESUME_REJECT:
824                 case MT_SUSPEND_ACKNOWLEDGE:
825                 case MT_SUSPEND_REJECT:
826                 case MT_USER_INFORMATION:
827                 case MT_RESTART:
828                 case MT_RESTART_ACKNOWLEDGE:
829                 case MT_CONGESTION_CONTROL:
830                 case MT_STATUS:
831                 case MT_STATUS_ENQUIRY:
832                         if (pc->debug & L3_DEB_CHECK)
833                                 l3_debug(pc->st, "l3dss1_check_messagetype_validity mt(%x) OK", mt);
834                         break;
835                 case MT_RESUME: /* RESUME only in user->net */
836                 case MT_SUSPEND: /* SUSPEND only in user->net */
837                 default:
838                         if (pc->debug & (L3_DEB_CHECK | L3_DEB_WARN))
839                                 l3_debug(pc->st, "l3dss1_check_messagetype_validity mt(%x) fail", mt);
840                         pc->para.cause = 97;
841                         l3dss1_status_send(pc, 0, NULL);
842                         return(1);
843         }
844         return(0);
845 }
846
847 static void
848 l3dss1_std_ie_err(struct l3_process *pc, int ret) {
849
850         if (pc->debug & L3_DEB_CHECK)
851                 l3_debug(pc->st, "check_infoelements ret %d", ret);
852         switch(ret) {
853                 case 0: 
854                         break;
855                 case ERR_IE_COMPREHENSION:
856                         pc->para.cause = 96;
857                         l3dss1_status_send(pc, 0, NULL);
858                         break;
859                 case ERR_IE_UNRECOGNIZED:
860                         pc->para.cause = 99;
861                         l3dss1_status_send(pc, 0, NULL);
862                         break;
863                 case ERR_IE_LENGTH:
864                         pc->para.cause = 100;
865                         l3dss1_status_send(pc, 0, NULL);
866                         break;
867                 case ERR_IE_SEQUENCE:
868                 default:
869                         break;
870         }
871 }
872
873 static int
874 l3dss1_get_channel_id(struct l3_process *pc, struct sk_buff *skb) {
875         u_char *p;
876
877         p = skb->data;
878         if ((p = findie(p, skb->len, IE_CHANNEL_ID, 0))) {
879                 p++;
880                 if (*p != 1) { /* len for BRI = 1 */
881                         if (pc->debug & L3_DEB_WARN)
882                                 l3_debug(pc->st, "wrong chid len %d", *p);
883                         return (-2);
884                 }
885                 p++;
886                 if (*p & 0x60) { /* only base rate interface */
887                         if (pc->debug & L3_DEB_WARN)
888                                 l3_debug(pc->st, "wrong chid %x", *p);
889                         return (-3);
890                 }
891                 return(*p & 0x3);
892         } else
893                 return(-1);
894 }
895
896 static int
897 l3dss1_get_cause(struct l3_process *pc, struct sk_buff *skb) {
898         u_char l, i=0;
899         u_char *p;
900
901         p = skb->data;
902         pc->para.cause = 31;
903         pc->para.loc = 0;
904         if ((p = findie(p, skb->len, IE_CAUSE, 0))) {
905                 p++;
906                 l = *p++;
907                 if (l>30)
908                         return(1);
909                 if (l) {
910                         pc->para.loc = *p++;
911                         l--;
912                 } else {
913                         return(2);
914                 }
915                 if (l && !(pc->para.loc & 0x80)) {
916                         l--;
917                         p++; /* skip recommendation */
918                 }
919                 if (l) {
920                         pc->para.cause = *p++;
921                         l--;
922                         if (!(pc->para.cause & 0x80))
923                                 return(3);
924                 } else
925                         return(4);
926                 while (l && (i<6)) {
927                         pc->para.diag[i++] = *p++;
928                         l--;
929                 }
930         } else
931                 return(-1);
932         return(0);
933 }
934
935 static void
936 l3dss1_msg_with_uus(struct l3_process *pc, u_char cmd)
937 {
938         struct sk_buff *skb;
939         u_char tmp[16+40];
940         u_char *p = tmp;
941         int l;
942
943         MsgHead(p, pc->callref, cmd);
944
945         if (pc->prot.dss1.uus1_data[0])
946          { *p++ = IE_USER_USER; /* UUS info element */
947            *p++ = strlen(pc->prot.dss1.uus1_data) + 1;
948            *p++ = 0x04; /* IA5 chars */
949            strcpy(p,pc->prot.dss1.uus1_data);
950            p += strlen(pc->prot.dss1.uus1_data);
951            pc->prot.dss1.uus1_data[0] = '\0';   
952          } 
953
954         l = p - tmp;
955         if (!(skb = l3_alloc_skb(l)))
956                 return;
957         memcpy(skb_put(skb, l), tmp, l);
958         l3_msg(pc->st, DL_DATA | REQUEST, skb);
959 } /* l3dss1_msg_with_uus */
960
961 static void
962 l3dss1_release_req(struct l3_process *pc, u_char pr, void *arg)
963 {
964         StopAllL3Timer(pc);
965         newl3state(pc, 19);
966         if (!pc->prot.dss1.uus1_data[0]) 
967                 l3dss1_message(pc, MT_RELEASE);
968         else
969                 l3dss1_msg_with_uus(pc, MT_RELEASE);
970         L3AddTimer(&pc->timer, T308, CC_T308_1);
971 }
972
973 static void
974 l3dss1_release_cmpl(struct l3_process *pc, u_char pr, void *arg)
975 {
976         struct sk_buff *skb = arg;
977         int ret;
978
979         if ((ret = l3dss1_get_cause(pc, skb))>0) {
980                 if (pc->debug & L3_DEB_WARN)
981                         l3_debug(pc->st, "RELCMPL get_cause ret(%d)",ret);
982         } else if (ret < 0)
983                 pc->para.cause = NO_CAUSE;
984         StopAllL3Timer(pc);
985         newl3state(pc, 0);
986         pc->st->l3.l3l4(pc->st, CC_RELEASE | CONFIRM, pc);
987         dss1_release_l3_process(pc);
988 }
989
990 #if EXT_BEARER_CAPS
991
992 static u_char *
993 EncodeASyncParams(u_char * p, u_char si2)
994 {                               // 7c 06 88  90 21 42 00 bb
995
996         p[0] = 0;
997         p[1] = 0x40;            // Intermediate rate: 16 kbit/s jj 2000.02.19
998         p[2] = 0x80;
999         if (si2 & 32)           // 7 data bits
1000
1001                 p[2] += 16;
1002         else                    // 8 data bits
1003
1004                 p[2] += 24;
1005
1006         if (si2 & 16)           // 2 stop bits
1007
1008                 p[2] += 96;
1009         else                    // 1 stop bit
1010
1011                 p[2] += 32;
1012
1013         if (si2 & 8)            // even parity
1014
1015                 p[2] += 2;
1016         else                    // no parity
1017
1018                 p[2] += 3;
1019
1020         switch (si2 & 0x07) {
1021                 case 0:
1022                         p[0] = 66;      // 1200 bit/s
1023
1024                         break;
1025                 case 1:
1026                         p[0] = 88;      // 1200/75 bit/s
1027
1028                         break;
1029                 case 2:
1030                         p[0] = 87;      // 75/1200 bit/s
1031
1032                         break;
1033                 case 3:
1034                         p[0] = 67;      // 2400 bit/s
1035
1036                         break;
1037                 case 4:
1038                         p[0] = 69;      // 4800 bit/s
1039
1040                         break;
1041                 case 5:
1042                         p[0] = 72;      // 9600 bit/s
1043
1044                         break;
1045                 case 6:
1046                         p[0] = 73;      // 14400 bit/s
1047
1048                         break;
1049                 case 7:
1050                         p[0] = 75;      // 19200 bit/s
1051
1052                         break;
1053         }
1054         return p + 3;
1055 }
1056
1057 static  u_char
1058 EncodeSyncParams(u_char si2, u_char ai)
1059 {
1060
1061         switch (si2) {
1062                 case 0:
1063                         return ai + 2;  // 1200 bit/s
1064
1065                 case 1:
1066                         return ai + 24;         // 1200/75 bit/s
1067
1068                 case 2:
1069                         return ai + 23;         // 75/1200 bit/s
1070
1071                 case 3:
1072                         return ai + 3;  // 2400 bit/s
1073
1074                 case 4:
1075                         return ai + 5;  // 4800 bit/s
1076
1077                 case 5:
1078                         return ai + 8;  // 9600 bit/s
1079
1080                 case 6:
1081                         return ai + 9;  // 14400 bit/s
1082
1083                 case 7:
1084                         return ai + 11;         // 19200 bit/s
1085
1086                 case 8:
1087                         return ai + 14;         // 48000 bit/s
1088
1089                 case 9:
1090                         return ai + 15;         // 56000 bit/s
1091
1092                 case 15:
1093                         return ai + 40;         // negotiate bit/s
1094
1095                 default:
1096                         break;
1097         }
1098         return ai;
1099 }
1100
1101
1102 static u_char
1103 DecodeASyncParams(u_char si2, u_char * p)
1104 {
1105         u_char info;
1106
1107         switch (p[5]) {
1108                 case 66:        // 1200 bit/s
1109
1110                         break;  // si2 don't change
1111
1112                 case 88:        // 1200/75 bit/s
1113
1114                         si2 += 1;
1115                         break;
1116                 case 87:        // 75/1200 bit/s
1117
1118                         si2 += 2;
1119                         break;
1120                 case 67:        // 2400 bit/s
1121
1122                         si2 += 3;
1123                         break;
1124                 case 69:        // 4800 bit/s
1125
1126                         si2 += 4;
1127                         break;
1128                 case 72:        // 9600 bit/s
1129
1130                         si2 += 5;
1131                         break;
1132                 case 73:        // 14400 bit/s
1133
1134                         si2 += 6;
1135                         break;
1136                 case 75:        // 19200 bit/s
1137
1138                         si2 += 7;
1139                         break;
1140         }
1141
1142         info = p[7] & 0x7f;
1143         if ((info & 16) && (!(info & 8)))       // 7 data bits
1144
1145                 si2 += 32;      // else 8 data bits
1146
1147         if ((info & 96) == 96)  // 2 stop bits
1148
1149                 si2 += 16;      // else 1 stop bit
1150
1151         if ((info & 2) && (!(info & 1)))        // even parity
1152
1153                 si2 += 8;       // else no parity
1154
1155         return si2;
1156 }
1157
1158
1159 static u_char
1160 DecodeSyncParams(u_char si2, u_char info)
1161 {
1162         info &= 0x7f;
1163         switch (info) {
1164                 case 40:        // bit/s negotiation failed  ai := 165 not 175!
1165
1166                         return si2 + 15;
1167                 case 15:        // 56000 bit/s failed, ai := 0 not 169 !
1168
1169                         return si2 + 9;
1170                 case 14:        // 48000 bit/s
1171
1172                         return si2 + 8;
1173                 case 11:        // 19200 bit/s
1174
1175                         return si2 + 7;
1176                 case 9: // 14400 bit/s
1177
1178                         return si2 + 6;
1179                 case 8: // 9600  bit/s
1180
1181                         return si2 + 5;
1182                 case 5: // 4800  bit/s
1183
1184                         return si2 + 4;
1185                 case 3: // 2400  bit/s
1186
1187                         return si2 + 3;
1188                 case 23:        // 75/1200 bit/s
1189
1190                         return si2 + 2;
1191                 case 24:        // 1200/75 bit/s
1192
1193                         return si2 + 1;
1194                 default:        // 1200 bit/s
1195
1196                         return si2;
1197         }
1198 }
1199
1200 static u_char
1201 DecodeSI2(struct sk_buff *skb)
1202 {
1203         u_char *p;              //, *pend=skb->data + skb->len;
1204
1205         if ((p = findie(skb->data, skb->len, 0x7c, 0))) {
1206                 switch (p[4] & 0x0f) {
1207                         case 0x01:
1208                                 if (p[1] == 0x04)       // sync. Bitratenadaption
1209
1210                                         return DecodeSyncParams(160, p[5]);     // V.110/X.30
1211
1212                                 else if (p[1] == 0x06)  // async. Bitratenadaption
1213
1214                                         return DecodeASyncParams(192, p);       // V.110/X.30
1215
1216                                 break;
1217                         case 0x08:      // if (p[5] == 0x02) // sync. Bitratenadaption
1218                                 if (p[1] > 3) 
1219                                         return DecodeSyncParams(176, p[5]);     // V.120
1220                                 break;
1221                 }
1222         }
1223         return 0;
1224 }
1225
1226 #endif
1227
1228
1229 static void
1230 l3dss1_setup_req(struct l3_process *pc, u_char pr,
1231                  void *arg)
1232 {
1233         struct sk_buff *skb;
1234         u_char tmp[128];
1235         u_char *p = tmp;
1236         u_char channel = 0;
1237
1238         u_char send_keypad;
1239         u_char screen = 0x80;
1240         u_char *teln;
1241         u_char *msn;
1242         u_char *sub;
1243         u_char *sp;
1244         int l;
1245
1246         MsgHead(p, pc->callref, MT_SETUP);
1247
1248         teln = pc->para.setup.phone;
1249 #ifndef CONFIG_HISAX_NO_KEYPAD
1250         send_keypad = (strchr(teln,'*') || strchr(teln,'#')) ? 1 : 0; 
1251 #else
1252         send_keypad = 0;
1253 #endif
1254 #ifndef CONFIG_HISAX_NO_SENDCOMPLETE
1255         if (!send_keypad)
1256                 *p++ = 0xa1;            /* complete indicator */
1257 #endif
1258         /*
1259          * Set Bearer Capability, Map info from 1TR6-convention to EDSS1
1260          */
1261         switch (pc->para.setup.si1) {
1262         case 1:                   /* Telephony                                */
1263                 *p++ = IE_BEARER;
1264                 *p++ = 0x3;       /* Length                                   */
1265                 *p++ = 0x90;      /* Coding Std. CCITT, 3.1 kHz audio         */
1266                 *p++ = 0x90;      /* Circuit-Mode 64kbps                      */
1267                 *p++ = 0xa3;      /* A-Law Audio                              */
1268                 break;
1269         case 5:                   /* Datatransmission 64k, BTX                */
1270         case 7:                   /* Datatransmission 64k                     */
1271         default:
1272                 *p++ = IE_BEARER;
1273                 *p++ = 0x2;       /* Length                                   */
1274                 *p++ = 0x88;      /* Coding Std. CCITT, unrestr. dig. Inform. */
1275                 *p++ = 0x90;      /* Circuit-Mode 64kbps                      */
1276                 break;
1277         }
1278
1279         if (send_keypad) {
1280                 *p++ = IE_KEYPAD;
1281                 *p++ = strlen(teln);
1282                 while (*teln)
1283                         *p++ = (*teln++) & 0x7F;
1284         }
1285           
1286         /*
1287          * What about info2? Mapping to High-Layer-Compatibility?
1288          */
1289         if ((*teln) && (!send_keypad)) {
1290                 /* parse number for special things */
1291                 if (!isdigit(*teln)) {
1292                         switch (0x5f & *teln) {
1293                                 case 'C':
1294                                         channel = 0x08;
1295                                 case 'P':
1296                                         channel |= 0x80;
1297                                         teln++;
1298                                         if (*teln == '1')
1299                                                 channel |= 0x01;
1300                                         else
1301                                                 channel |= 0x02;
1302                                         break;
1303                                 case 'R':
1304                                         screen = 0xA0;
1305                                         break;
1306                                 case 'D':
1307                                         screen = 0x80;
1308                                         break;
1309                                 
1310                                 default:
1311                                         if (pc->debug & L3_DEB_WARN)
1312                                                 l3_debug(pc->st, "Wrong MSN Code");
1313                                         break;
1314                         }
1315                         teln++;
1316                 }
1317         }
1318         if (channel) {
1319                 *p++ = IE_CHANNEL_ID;
1320                 *p++ = 1;
1321                 *p++ = channel;
1322         }
1323         msn = pc->para.setup.eazmsn;
1324         sub = NULL;
1325         sp = msn;
1326         while (*sp) {
1327                 if ('.' == *sp) {
1328                         sub = sp;
1329                         *sp = 0;
1330                 } else
1331                         sp++;
1332         }
1333         if (*msn) {
1334                 *p++ = IE_CALLING_PN;
1335                 *p++ = strlen(msn) + (screen ? 2 : 1);
1336                 /* Classify as AnyPref. */
1337                 if (screen) {
1338                         *p++ = 0x01;    /* Ext = '0'B, Type = '000'B, Plan = '0001'B. */
1339                         *p++ = screen;
1340                 } else
1341                         *p++ = 0x81;    /* Ext = '1'B, Type = '000'B, Plan = '0001'B. */
1342                 while (*msn)
1343                         *p++ = *msn++ & 0x7f;
1344         }
1345         if (sub) {
1346                 *sub++ = '.';
1347                 *p++ = IE_CALLING_SUB;
1348                 *p++ = strlen(sub) + 2;
1349                 *p++ = 0x80;    /* NSAP coded */
1350                 *p++ = 0x50;    /* local IDI format */
1351                 while (*sub)
1352                         *p++ = *sub++ & 0x7f;
1353         }
1354         sub = NULL;
1355         sp = teln;
1356         while (*sp) {
1357                 if ('.' == *sp) {
1358                         sub = sp;
1359                         *sp = 0;
1360                 } else
1361                         sp++;
1362         }
1363         
1364         if (!send_keypad) {      
1365                 *p++ = IE_CALLED_PN;
1366                 *p++ = strlen(teln) + 1;
1367                 /* Classify as AnyPref. */
1368                 *p++ = 0x81;            /* Ext = '1'B, Type = '000'B, Plan = '0001'B. */
1369                 while (*teln)
1370                         *p++ = *teln++ & 0x7f;
1371                 
1372                 if (sub) {
1373                         *sub++ = '.';
1374                         *p++ = IE_CALLED_SUB;
1375                         *p++ = strlen(sub) + 2;
1376                         *p++ = 0x80;    /* NSAP coded */
1377                         *p++ = 0x50;    /* local IDI format */
1378                         while (*sub)
1379                                 *p++ = *sub++ & 0x7f;
1380                 }
1381         }
1382 #if EXT_BEARER_CAPS
1383         if ((pc->para.setup.si2 >= 160) && (pc->para.setup.si2 <= 175)) {       // sync. Bitratenadaption, V.110/X.30
1384
1385                 *p++ = IE_LLC;
1386                 *p++ = 0x04;
1387                 *p++ = 0x88;
1388                 *p++ = 0x90;
1389                 *p++ = 0x21;
1390                 *p++ = EncodeSyncParams(pc->para.setup.si2 - 160, 0x80);
1391         } else if ((pc->para.setup.si2 >= 176) && (pc->para.setup.si2 <= 191)) {        // sync. Bitratenadaption, V.120
1392
1393                 *p++ = IE_LLC;
1394                 *p++ = 0x05;
1395                 *p++ = 0x88;
1396                 *p++ = 0x90;
1397                 *p++ = 0x28;
1398                 *p++ = EncodeSyncParams(pc->para.setup.si2 - 176, 0);
1399                 *p++ = 0x82;
1400         } else if (pc->para.setup.si2 >= 192) {         // async. Bitratenadaption, V.110/X.30
1401
1402                 *p++ = IE_LLC;
1403                 *p++ = 0x06;
1404                 *p++ = 0x88;
1405                 *p++ = 0x90;
1406                 *p++ = 0x21;
1407                 p = EncodeASyncParams(p, pc->para.setup.si2 - 192);
1408 #ifndef CONFIG_HISAX_NO_LLC
1409         } else {
1410           switch (pc->para.setup.si1) {
1411                 case 1:                 /* Telephony                                */
1412                         *p++ = IE_LLC;
1413                         *p++ = 0x3;     /* Length                                   */
1414                         *p++ = 0x90;    /* Coding Std. CCITT, 3.1 kHz audio         */
1415                         *p++ = 0x90;    /* Circuit-Mode 64kbps                      */
1416                         *p++ = 0xa3;    /* A-Law Audio                              */
1417                         break;
1418                 case 5:                 /* Datatransmission 64k, BTX                */
1419                 case 7:                 /* Datatransmission 64k                     */
1420                 default:
1421                         *p++ = IE_LLC;
1422                         *p++ = 0x2;     /* Length                                   */
1423                         *p++ = 0x88;    /* Coding Std. CCITT, unrestr. dig. Inform. */
1424                         *p++ = 0x90;    /* Circuit-Mode 64kbps                      */
1425                         break;
1426           }
1427 #endif
1428         }
1429 #endif
1430         l = p - tmp;
1431         if (!(skb = l3_alloc_skb(l)))
1432                 return;
1433         memcpy(skb_put(skb, l), tmp, l);
1434         L3DelTimer(&pc->timer);
1435         L3AddTimer(&pc->timer, T303, CC_T303);
1436         newl3state(pc, 1);
1437         l3_msg(pc->st, DL_DATA | REQUEST, skb);
1438 }
1439
1440 static void
1441 l3dss1_call_proc(struct l3_process *pc, u_char pr, void *arg)
1442 {
1443         struct sk_buff *skb = arg;
1444         int id, ret;
1445
1446         if ((id = l3dss1_get_channel_id(pc, skb)) >= 0) {
1447                 if ((0 == id) || ((3 == id) && (0x10 == pc->para.moderate))) {
1448                         if (pc->debug & L3_DEB_WARN)
1449                                 l3_debug(pc->st, "setup answer with wrong chid %x", id);
1450                         pc->para.cause = 100;
1451                         l3dss1_status_send(pc, pr, NULL);
1452                         return;
1453                 }
1454                 pc->para.bchannel = id;
1455         } else if (1 == pc->state) {
1456                 if (pc->debug & L3_DEB_WARN)
1457                         l3_debug(pc->st, "setup answer wrong chid (ret %d)", id);
1458                 if (id == -1)
1459                         pc->para.cause = 96;
1460                 else
1461                         pc->para.cause = 100;
1462                 l3dss1_status_send(pc, pr, NULL);
1463                 return;
1464         }
1465         /* Now we are on none mandatory IEs */
1466         ret = check_infoelements(pc, skb, ie_CALL_PROCEEDING);
1467         if (ERR_IE_COMPREHENSION == ret) {
1468                 l3dss1_std_ie_err(pc, ret);
1469                 return;
1470         }
1471         L3DelTimer(&pc->timer);
1472         newl3state(pc, 3);
1473         L3AddTimer(&pc->timer, T310, CC_T310);
1474         if (ret) /* STATUS for none mandatory IE errors after actions are taken */
1475                 l3dss1_std_ie_err(pc, ret);
1476         pc->st->l3.l3l4(pc->st, CC_PROCEEDING | INDICATION, pc);
1477 }
1478
1479 static void
1480 l3dss1_setup_ack(struct l3_process *pc, u_char pr, void *arg)
1481 {
1482         struct sk_buff *skb = arg;
1483         int id, ret;
1484
1485         if ((id = l3dss1_get_channel_id(pc, skb)) >= 0) {
1486                 if ((0 == id) || ((3 == id) && (0x10 == pc->para.moderate))) {
1487                         if (pc->debug & L3_DEB_WARN)
1488                                 l3_debug(pc->st, "setup answer with wrong chid %x", id);
1489                         pc->para.cause = 100;
1490                         l3dss1_status_send(pc, pr, NULL);
1491                         return;
1492                 }
1493                 pc->para.bchannel = id;
1494         } else {
1495                 if (pc->debug & L3_DEB_WARN)
1496                         l3_debug(pc->st, "setup answer wrong chid (ret %d)", id);
1497                 if (id == -1)
1498                         pc->para.cause = 96;
1499                 else
1500                         pc->para.cause = 100;
1501                 l3dss1_status_send(pc, pr, NULL);
1502                 return;
1503         }
1504         /* Now we are on none mandatory IEs */
1505         ret = check_infoelements(pc, skb, ie_SETUP_ACKNOWLEDGE);
1506         if (ERR_IE_COMPREHENSION == ret) {
1507                 l3dss1_std_ie_err(pc, ret);
1508                 return;
1509         }
1510         L3DelTimer(&pc->timer);
1511         newl3state(pc, 2);
1512         L3AddTimer(&pc->timer, T304, CC_T304);
1513         if (ret) /* STATUS for none mandatory IE errors after actions are taken */
1514                 l3dss1_std_ie_err(pc, ret);
1515         pc->st->l3.l3l4(pc->st, CC_MORE_INFO | INDICATION, pc);
1516 }
1517
1518 static void
1519 l3dss1_disconnect(struct l3_process *pc, u_char pr, void *arg)
1520 {
1521         struct sk_buff *skb = arg;
1522         u_char *p;
1523         int ret;
1524         u_char cause = 0;
1525
1526         StopAllL3Timer(pc);
1527         if ((ret = l3dss1_get_cause(pc, skb))) {
1528                 if (pc->debug & L3_DEB_WARN)
1529                         l3_debug(pc->st, "DISC get_cause ret(%d)", ret);
1530                 if (ret < 0)
1531                         cause = 96;
1532                 else if (ret > 0)
1533                         cause = 100;
1534         } 
1535         if ((p = findie(skb->data, skb->len, IE_FACILITY, 0)))
1536                 l3dss1_parse_facility(pc->st, pc, pc->callref, p);
1537         ret = check_infoelements(pc, skb, ie_DISCONNECT);
1538         if (ERR_IE_COMPREHENSION == ret)
1539                 cause = 96;
1540         else if ((!cause) && (ERR_IE_UNRECOGNIZED == ret))
1541                 cause = 99;
1542         ret = pc->state;
1543         newl3state(pc, 12);
1544         if (cause)
1545                 newl3state(pc, 19);
1546         if (11 != ret)
1547                 pc->st->l3.l3l4(pc->st, CC_DISCONNECT | INDICATION, pc);
1548         else if (!cause)
1549                    l3dss1_release_req(pc, pr, NULL);
1550         if (cause) {
1551                 l3dss1_message_cause(pc, MT_RELEASE, cause);
1552                 L3AddTimer(&pc->timer, T308, CC_T308_1);
1553         }
1554 }
1555
1556 static void
1557 l3dss1_connect(struct l3_process *pc, u_char pr, void *arg)
1558 {
1559         struct sk_buff *skb = arg;
1560         int ret;
1561
1562         ret = check_infoelements(pc, skb, ie_CONNECT);
1563         if (ERR_IE_COMPREHENSION == ret) {
1564                 l3dss1_std_ie_err(pc, ret);
1565                 return;
1566         }
1567         L3DelTimer(&pc->timer); /* T310 */
1568         newl3state(pc, 10);
1569         pc->para.chargeinfo = 0;
1570         /* here should inserted COLP handling KKe */
1571         if (ret)
1572                 l3dss1_std_ie_err(pc, ret);
1573         pc->st->l3.l3l4(pc->st, CC_SETUP | CONFIRM, pc);
1574 }
1575
1576 static void
1577 l3dss1_alerting(struct l3_process *pc, u_char pr, void *arg)
1578 {
1579         struct sk_buff *skb = arg;
1580         int ret;
1581
1582         ret = check_infoelements(pc, skb, ie_ALERTING);
1583         if (ERR_IE_COMPREHENSION == ret) {
1584                 l3dss1_std_ie_err(pc, ret);
1585                 return;
1586         }
1587         L3DelTimer(&pc->timer); /* T304 */
1588         newl3state(pc, 4);
1589         if (ret)
1590                 l3dss1_std_ie_err(pc, ret);
1591         pc->st->l3.l3l4(pc->st, CC_ALERTING | INDICATION, pc);
1592 }
1593
1594 static void
1595 l3dss1_setup(struct l3_process *pc, u_char pr, void *arg)
1596 {
1597         u_char *p;
1598         int bcfound = 0;
1599         char tmp[80];
1600         struct sk_buff *skb = arg;
1601         int id;
1602         int err = 0;
1603
1604         /*
1605          * Bearer Capabilities
1606          */
1607         p = skb->data;
1608         /* only the first occurence 'll be detected ! */
1609         if ((p = findie(p, skb->len, 0x04, 0))) {
1610                 if ((p[1] < 2) || (p[1] > 11))
1611                         err = 1;
1612                 else {
1613                         pc->para.setup.si2 = 0;
1614                         switch (p[2] & 0x7f) {
1615                                 case 0x00: /* Speech */
1616                                 case 0x10: /* 3.1 Khz audio */
1617                                         pc->para.setup.si1 = 1;
1618                                         break;
1619                                 case 0x08: /* Unrestricted digital information */
1620                                         pc->para.setup.si1 = 7;
1621 /* JIM, 05.11.97 I wanna set service indicator 2 */
1622 #if EXT_BEARER_CAPS
1623                                         pc->para.setup.si2 = DecodeSI2(skb);
1624 #endif
1625                                         break;
1626                                 case 0x09: /* Restricted digital information */
1627                                         pc->para.setup.si1 = 2;
1628                                         break;
1629                                 case 0x11:
1630                                         /* Unrestr. digital information  with 
1631                                          * tones/announcements ( or 7 kHz audio
1632                                          */
1633                                         pc->para.setup.si1 = 3;
1634                                         break;
1635                                 case 0x18: /* Video */
1636                                         pc->para.setup.si1 = 4;
1637                                         break;
1638                                 default:
1639                                         err = 2;
1640                                         break;
1641                         }
1642                         switch (p[3] & 0x7f) {
1643                                 case 0x40: /* packed mode */
1644                                         pc->para.setup.si1 = 8;
1645                                         break;
1646                                 case 0x10: /* 64 kbit */
1647                                 case 0x11: /* 2*64 kbit */
1648                                 case 0x13: /* 384 kbit */
1649                                 case 0x15: /* 1536 kbit */
1650                                 case 0x17: /* 1920 kbit */
1651                                         pc->para.moderate = p[3] & 0x7f;
1652                                         break;
1653                                 default:
1654                                         err = 3;
1655                                         break;
1656                         }
1657                 }
1658                 if (pc->debug & L3_DEB_SI)
1659                         l3_debug(pc->st, "SI=%d, AI=%d",
1660                                 pc->para.setup.si1, pc->para.setup.si2);
1661                 if (err) {
1662                         if (pc->debug & L3_DEB_WARN)
1663                                 l3_debug(pc->st, "setup with wrong bearer(l=%d:%x,%x)",
1664                                         p[1], p[2], p[3]);
1665                         pc->para.cause = 100;
1666                         l3dss1_msg_without_setup(pc, pr, NULL);
1667                         return;
1668                 }
1669         } else {
1670                 if (pc->debug & L3_DEB_WARN)
1671                         l3_debug(pc->st, "setup without bearer capabilities");
1672                 /* ETS 300-104 1.3.3 */
1673                 pc->para.cause = 96;
1674                 l3dss1_msg_without_setup(pc, pr, NULL);
1675                 return;
1676         }
1677         /*
1678          * Channel Identification
1679          */
1680         if ((id = l3dss1_get_channel_id(pc, skb)) >= 0) {
1681                 if ((pc->para.bchannel = id)) {
1682                         if ((3 == id) && (0x10 == pc->para.moderate)) {
1683                                 if (pc->debug & L3_DEB_WARN)
1684                                         l3_debug(pc->st, "setup with wrong chid %x",
1685                                                 id);
1686                                 pc->para.cause = 100;
1687                                 l3dss1_msg_without_setup(pc, pr, NULL);
1688                                 return;
1689                         }
1690                         bcfound++;
1691                 } else 
1692                    { if (pc->debug & L3_DEB_WARN)
1693                          l3_debug(pc->st, "setup without bchannel, call waiting");
1694                      bcfound++;
1695                    } 
1696         } else {
1697                 if (pc->debug & L3_DEB_WARN)
1698                         l3_debug(pc->st, "setup with wrong chid ret %d", id);
1699                 if (id == -1)
1700                         pc->para.cause = 96;
1701                 else
1702                         pc->para.cause = 100;
1703                 l3dss1_msg_without_setup(pc, pr, NULL);
1704                 return;
1705         }
1706         /* Now we are on none mandatory IEs */
1707         err = check_infoelements(pc, skb, ie_SETUP);
1708         if (ERR_IE_COMPREHENSION == err) {
1709                 pc->para.cause = 96;
1710                 l3dss1_msg_without_setup(pc, pr, NULL);
1711                 return;
1712         }
1713         p = skb->data;
1714         if ((p = findie(p, skb->len, 0x70, 0)))
1715                 iecpy(pc->para.setup.eazmsn, p, 1);
1716         else
1717                 pc->para.setup.eazmsn[0] = 0;
1718
1719         p = skb->data;
1720         if ((p = findie(p, skb->len, 0x71, 0))) {
1721                 /* Called party subaddress */
1722                 if ((p[1] >= 2) && (p[2] == 0x80) && (p[3] == 0x50)) {
1723                         tmp[0] = '.';
1724                         iecpy(&tmp[1], p, 2);
1725                         strcat(pc->para.setup.eazmsn, tmp);
1726                 } else if (pc->debug & L3_DEB_WARN)
1727                         l3_debug(pc->st, "wrong called subaddress");
1728         }
1729         p = skb->data;
1730         if ((p = findie(p, skb->len, 0x6c, 0))) {
1731                 pc->para.setup.plan = p[2];
1732                 if (p[2] & 0x80) {
1733                         iecpy(pc->para.setup.phone, p, 1);
1734                         pc->para.setup.screen = 0;
1735                 } else {
1736                         iecpy(pc->para.setup.phone, p, 2);
1737                         pc->para.setup.screen = p[3];
1738                 }
1739         } else {
1740                 pc->para.setup.phone[0] = 0;
1741                 pc->para.setup.plan = 0;
1742                 pc->para.setup.screen = 0;
1743         }
1744         p = skb->data;
1745         if ((p = findie(p, skb->len, 0x6d, 0))) {
1746                 /* Calling party subaddress */
1747                 if ((p[1] >= 2) && (p[2] == 0x80) && (p[3] == 0x50)) {
1748                         tmp[0] = '.';
1749                         iecpy(&tmp[1], p, 2);
1750                         strcat(pc->para.setup.phone, tmp);
1751                 } else if (pc->debug & L3_DEB_WARN)
1752                         l3_debug(pc->st, "wrong calling subaddress");
1753         }
1754         newl3state(pc, 6);
1755         if (err) /* STATUS for none mandatory IE errors after actions are taken */
1756                 l3dss1_std_ie_err(pc, err);
1757         pc->st->l3.l3l4(pc->st, CC_SETUP | INDICATION, pc);
1758 }
1759
1760 static void
1761 l3dss1_reset(struct l3_process *pc, u_char pr, void *arg)
1762 {
1763         dss1_release_l3_process(pc);
1764 }
1765
1766 static void
1767 l3dss1_disconnect_req(struct l3_process *pc, u_char pr, void *arg)
1768 {
1769         struct sk_buff *skb;
1770         u_char tmp[16+40];
1771         u_char *p = tmp;
1772         int l;
1773         u_char cause = 16;
1774
1775         if (pc->para.cause != NO_CAUSE)
1776                 cause = pc->para.cause;
1777
1778         StopAllL3Timer(pc);
1779
1780         MsgHead(p, pc->callref, MT_DISCONNECT);
1781
1782         *p++ = IE_CAUSE;
1783         *p++ = 0x2;
1784         *p++ = 0x80;
1785         *p++ = cause | 0x80;
1786
1787         if (pc->prot.dss1.uus1_data[0])
1788          { *p++ = IE_USER_USER; /* UUS info element */
1789            *p++ = strlen(pc->prot.dss1.uus1_data) + 1;
1790            *p++ = 0x04; /* IA5 chars */
1791            strcpy(p,pc->prot.dss1.uus1_data);
1792            p += strlen(pc->prot.dss1.uus1_data);
1793            pc->prot.dss1.uus1_data[0] = '\0';   
1794          } 
1795
1796         l = p - tmp;
1797         if (!(skb = l3_alloc_skb(l)))
1798                 return;
1799         memcpy(skb_put(skb, l), tmp, l);
1800         newl3state(pc, 11);
1801         l3_msg(pc->st, DL_DATA | REQUEST, skb);
1802         L3AddTimer(&pc->timer, T305, CC_T305);
1803 }
1804
1805 static void
1806 l3dss1_setup_rsp(struct l3_process *pc, u_char pr,
1807                  void *arg)
1808 {
1809         if (!pc->para.bchannel) 
1810          { if (pc->debug & L3_DEB_WARN)
1811                l3_debug(pc->st, "D-chan connect for waiting call");
1812            l3dss1_disconnect_req(pc, pr, arg);
1813            return;
1814          }
1815         newl3state(pc, 8);
1816         l3dss1_message(pc, MT_CONNECT);
1817         L3DelTimer(&pc->timer);
1818         L3AddTimer(&pc->timer, T313, CC_T313);
1819 }
1820
1821 static void
1822 l3dss1_connect_ack(struct l3_process *pc, u_char pr, void *arg)
1823 {
1824         struct sk_buff *skb = arg;
1825         int ret;
1826
1827         ret = check_infoelements(pc, skb, ie_CONNECT_ACKNOWLEDGE);
1828         if (ERR_IE_COMPREHENSION == ret) {
1829                 l3dss1_std_ie_err(pc, ret);
1830                 return;
1831         }
1832         newl3state(pc, 10);
1833         L3DelTimer(&pc->timer);
1834         if (ret)
1835                 l3dss1_std_ie_err(pc, ret);
1836         pc->st->l3.l3l4(pc->st, CC_SETUP_COMPL | INDICATION, pc);
1837 }
1838
1839 static void
1840 l3dss1_reject_req(struct l3_process *pc, u_char pr, void *arg)
1841 {
1842         struct sk_buff *skb;
1843         u_char tmp[16];
1844         u_char *p = tmp;
1845         int l;
1846         u_char cause = 21;
1847
1848         if (pc->para.cause != NO_CAUSE)
1849                 cause = pc->para.cause;
1850
1851         MsgHead(p, pc->callref, MT_RELEASE_COMPLETE);
1852
1853         *p++ = IE_CAUSE;
1854         *p++ = 0x2;
1855         *p++ = 0x80;
1856         *p++ = cause | 0x80;
1857
1858         l = p - tmp;
1859         if (!(skb = l3_alloc_skb(l)))
1860                 return;
1861         memcpy(skb_put(skb, l), tmp, l);
1862         l3_msg(pc->st, DL_DATA | REQUEST, skb);
1863         pc->st->l3.l3l4(pc->st, CC_RELEASE | INDICATION, pc);
1864         newl3state(pc, 0);
1865         dss1_release_l3_process(pc);
1866 }
1867
1868 static void
1869 l3dss1_release(struct l3_process *pc, u_char pr, void *arg)
1870 {
1871         struct sk_buff *skb = arg;
1872         u_char *p;
1873         int ret, cause=0;
1874
1875         StopAllL3Timer(pc);
1876         if ((ret = l3dss1_get_cause(pc, skb))>0) {
1877                 if (pc->debug & L3_DEB_WARN)
1878                         l3_debug(pc->st, "REL get_cause ret(%d)", ret);
1879         } else if (ret<0)
1880                 pc->para.cause = NO_CAUSE;
1881         if ((p = findie(skb->data, skb->len, IE_FACILITY, 0))) {
1882                 l3dss1_parse_facility(pc->st, pc, pc->callref, p);
1883         }
1884         if ((ret<0) && (pc->state != 11))
1885                 cause = 96;
1886         else if (ret>0)
1887                 cause = 100;
1888         ret = check_infoelements(pc, skb, ie_RELEASE);
1889         if (ERR_IE_COMPREHENSION == ret)
1890                 cause = 96;
1891         else if ((ERR_IE_UNRECOGNIZED == ret) && (!cause))
1892                 cause = 99;  
1893         if (cause)
1894                 l3dss1_message_cause(pc, MT_RELEASE_COMPLETE, cause);
1895         else
1896                 l3dss1_message(pc, MT_RELEASE_COMPLETE);
1897         pc->st->l3.l3l4(pc->st, CC_RELEASE | INDICATION, pc);
1898         newl3state(pc, 0);
1899         dss1_release_l3_process(pc);
1900 }
1901
1902 static void
1903 l3dss1_alert_req(struct l3_process *pc, u_char pr,
1904                  void *arg)
1905 {
1906         newl3state(pc, 7);
1907         if (!pc->prot.dss1.uus1_data[0]) 
1908                 l3dss1_message(pc, MT_ALERTING);
1909         else
1910                 l3dss1_msg_with_uus(pc, MT_ALERTING); 
1911 }
1912
1913 static void
1914 l3dss1_proceed_req(struct l3_process *pc, u_char pr,
1915                    void *arg)
1916 {
1917         newl3state(pc, 9);
1918         l3dss1_message(pc, MT_CALL_PROCEEDING);
1919         pc->st->l3.l3l4(pc->st, CC_PROCEED_SEND | INDICATION, pc); 
1920 }
1921
1922 static void
1923 l3dss1_setup_ack_req(struct l3_process *pc, u_char pr,
1924                    void *arg)
1925 {
1926         newl3state(pc, 25);
1927         L3DelTimer(&pc->timer);
1928         L3AddTimer(&pc->timer, T302, CC_T302);
1929         l3dss1_message(pc, MT_SETUP_ACKNOWLEDGE);
1930 }
1931
1932 /********************************************/
1933 /* deliver a incoming display message to HL */
1934 /********************************************/
1935 static void
1936 l3dss1_deliver_display(struct l3_process *pc, int pr, u_char *infp)
1937 {       u_char len;
1938         isdn_ctrl ic; 
1939         struct IsdnCardState *cs;
1940         char *p; 
1941
1942         if (*infp++ != IE_DISPLAY) return;
1943         if ((len = *infp++) > 80) return; /* total length <= 82 */
1944         if (!pc->chan) return;
1945
1946         p = ic.parm.display; 
1947         while (len--)
1948           *p++ = *infp++;
1949         *p = '\0';
1950         ic.command = ISDN_STAT_DISPLAY;
1951         cs = pc->st->l1.hardware;
1952         ic.driver = cs->myid;
1953         ic.arg = pc->chan->chan; 
1954         cs->iif.statcallb(&ic);
1955 } /* l3dss1_deliver_display */
1956
1957
1958 static void
1959 l3dss1_progress(struct l3_process *pc, u_char pr, void *arg)
1960 {
1961         struct sk_buff *skb = arg;
1962         int err = 0;
1963         u_char *p;
1964
1965         if ((p = findie(skb->data, skb->len, IE_PROGRESS, 0))) {
1966                 if (p[1] != 2) {
1967                         err = 1;
1968                         pc->para.cause = 100;
1969                 } else if (!(p[2] & 0x70)) {
1970                         switch (p[2]) {
1971                                 case 0x80:
1972                                 case 0x81:
1973                                 case 0x82:
1974                                 case 0x84:
1975                                 case 0x85:
1976                                 case 0x87:
1977                                 case 0x8a:
1978                                         switch (p[3]) {
1979                                                 case 0x81:
1980                                                 case 0x82:
1981                                                 case 0x83:
1982                                                 case 0x84:
1983                                                 case 0x88:
1984                                                         break;
1985                                                 default:
1986                                                         err = 2;
1987                                                         pc->para.cause = 100;
1988                                                         break;
1989                                         }
1990                                         break;
1991                                 default:
1992                                         err = 3;
1993                                         pc->para.cause = 100;
1994                                         break;
1995                         }
1996                 }
1997         } else {
1998                 pc->para.cause = 96;
1999                 err = 4;
2000         }
2001         if (err) {      
2002                 if (pc->debug & L3_DEB_WARN)
2003                         l3_debug(pc->st, "progress error %d", err);
2004                 l3dss1_status_send(pc, pr, NULL);
2005                 return;
2006         }
2007         /* Now we are on none mandatory IEs */
2008         err = check_infoelements(pc, skb, ie_PROGRESS);
2009         if (err)
2010                 l3dss1_std_ie_err(pc, err);
2011         if (ERR_IE_COMPREHENSION != err)
2012                 pc->st->l3.l3l4(pc->st, CC_PROGRESS | INDICATION, pc);
2013 }
2014
2015 static void
2016 l3dss1_notify(struct l3_process *pc, u_char pr, void *arg)
2017 {
2018         struct sk_buff *skb = arg;
2019         int err = 0;
2020         u_char *p;
2021
2022         if ((p = findie(skb->data, skb->len, IE_NOTIFY, 0))) {
2023                 if (p[1] != 1) {
2024                         err = 1;
2025                         pc->para.cause = 100;
2026                 } else {
2027                         switch (p[2]) {
2028                                 case 0x80:
2029                                 case 0x81:
2030                                 case 0x82:
2031                                         break;
2032                                 default:
2033                                         pc->para.cause = 100;
2034                                         err = 2;
2035                                         break;
2036                         }
2037                 }
2038         } else {
2039                 pc->para.cause = 96;
2040                 err = 3;
2041         }
2042         if (err) {      
2043                 if (pc->debug & L3_DEB_WARN)
2044                         l3_debug(pc->st, "notify error %d", err);
2045                 l3dss1_status_send(pc, pr, NULL);
2046                 return;
2047         }
2048         /* Now we are on none mandatory IEs */
2049         err = check_infoelements(pc, skb, ie_NOTIFY);
2050         if (err)
2051                 l3dss1_std_ie_err(pc, err);
2052         if (ERR_IE_COMPREHENSION != err)
2053                 pc->st->l3.l3l4(pc->st, CC_NOTIFY | INDICATION, pc);
2054 }
2055
2056 static void
2057 l3dss1_status_enq(struct l3_process *pc, u_char pr, void *arg)
2058 {
2059         int ret;
2060         struct sk_buff *skb = arg;
2061
2062         ret = check_infoelements(pc, skb, ie_STATUS_ENQUIRY);
2063         l3dss1_std_ie_err(pc, ret);
2064         pc->para.cause = 30; /* response to STATUS_ENQUIRY */
2065         l3dss1_status_send(pc, pr, NULL);
2066 }
2067
2068 static void
2069 l3dss1_information(struct l3_process *pc, u_char pr, void *arg)
2070 {
2071         int ret;
2072         struct sk_buff *skb = arg;
2073         u_char *p;
2074         char tmp[32];
2075
2076         ret = check_infoelements(pc, skb, ie_INFORMATION);
2077         if (ret)
2078                 l3dss1_std_ie_err(pc, ret);
2079         if (pc->state == 25) { /* overlap receiving */
2080                 L3DelTimer(&pc->timer);
2081                 p = skb->data;
2082                 if ((p = findie(p, skb->len, 0x70, 0))) {
2083                         iecpy(tmp, p, 1);
2084                         strcat(pc->para.setup.eazmsn, tmp);
2085                         pc->st->l3.l3l4(pc->st, CC_MORE_INFO | INDICATION, pc);
2086                 }
2087                 L3AddTimer(&pc->timer, T302, CC_T302);
2088         }
2089 }
2090
2091 /******************************/
2092 /* handle deflection requests */
2093 /******************************/
2094 static void l3dss1_redir_req(struct l3_process *pc, u_char pr, void *arg)
2095 {
2096         struct sk_buff *skb;
2097         u_char tmp[128];
2098         u_char *p = tmp;
2099         u_char *subp;
2100         u_char len_phone = 0;
2101         u_char len_sub = 0;
2102         int l; 
2103
2104
2105         strcpy(pc->prot.dss1.uus1_data,pc->chan->setup.eazmsn); /* copy uus element if available */
2106         if (!pc->chan->setup.phone[0])
2107           { pc->para.cause = -1;
2108             l3dss1_disconnect_req(pc,pr,arg); /* disconnect immediately */
2109             return;
2110           } /* only uus */
2111  
2112         if (pc->prot.dss1.invoke_id) 
2113           free_invoke_id(pc->st,pc->prot.dss1.invoke_id);
2114  
2115         if (!(pc->prot.dss1.invoke_id = new_invoke_id(pc->st))) 
2116           return;
2117
2118         MsgHead(p, pc->callref, MT_FACILITY);
2119
2120         for (subp = pc->chan->setup.phone; (*subp) && (*subp != '.'); subp++) len_phone++; /* len of phone number */
2121         if (*subp++ == '.') len_sub = strlen(subp) + 2; /* length including info subaddress element */ 
2122
2123         *p++ = 0x1c;   /* Facility info element */
2124         *p++ = len_phone + len_sub + 2 + 2 + 8 + 3 + 3; /* length of element */
2125         *p++ = 0x91;  /* remote operations protocol */
2126         *p++ = 0xa1;  /* invoke component */
2127           
2128         *p++ = len_phone + len_sub + 2 + 2 + 8 + 3; /* length of data */
2129         *p++ = 0x02;  /* invoke id tag, integer */
2130         *p++ = 0x01;  /* length */
2131         *p++ = pc->prot.dss1.invoke_id;  /* invoke id */ 
2132         *p++ = 0x02;  /* operation value tag, integer */
2133         *p++ = 0x01;  /* length */
2134         *p++ = 0x0D;  /* Call Deflect */
2135           
2136         *p++ = 0x30;  /* sequence phone number */
2137         *p++ = len_phone + 2 + 2 + 3 + len_sub; /* length */
2138           
2139         *p++ = 0x30;  /* Deflected to UserNumber */
2140         *p++ = len_phone+2+len_sub; /* length */
2141         *p++ = 0x80; /* NumberDigits */
2142         *p++ = len_phone; /* length */
2143         for (l = 0; l < len_phone; l++)
2144          *p++ = pc->chan->setup.phone[l];
2145
2146         if (len_sub)
2147           { *p++ = 0x04; /* called party subaddress */
2148             *p++ = len_sub - 2;
2149             while (*subp) *p++ = *subp++;
2150           }
2151
2152         *p++ = 0x01; /* screening identifier */
2153         *p++ = 0x01;
2154         *p++ = pc->chan->setup.screen;
2155
2156         l = p - tmp;
2157         if (!(skb = l3_alloc_skb(l))) return;
2158         memcpy(skb_put(skb, l), tmp, l);
2159
2160         l3_msg(pc->st, DL_DATA | REQUEST, skb);
2161 } /* l3dss1_redir_req */
2162
2163 /********************************************/
2164 /* handle deflection request in early state */
2165 /********************************************/
2166 static void l3dss1_redir_req_early(struct l3_process *pc, u_char pr, void *arg)
2167 {
2168   l3dss1_proceed_req(pc,pr,arg);
2169   l3dss1_redir_req(pc,pr,arg);
2170 } /* l3dss1_redir_req_early */
2171
2172 /***********************************************/
2173 /* handle special commands for this protocol.  */
2174 /* Examples are call independant services like */
2175 /* remote operations with dummy  callref.      */
2176 /***********************************************/
2177 static int l3dss1_cmd_global(struct PStack *st, isdn_ctrl *ic)
2178 { u_char id;
2179   u_char temp[265];
2180   u_char *p = temp;
2181   int i, l, proc_len; 
2182   struct sk_buff *skb;
2183   struct l3_process *pc = NULL;
2184
2185   switch (ic->arg)
2186    { case DSS1_CMD_INVOKE:
2187        if (ic->parm.dss1_io.datalen < 0) return(-2); /* invalid parameter */ 
2188
2189        for (proc_len = 1, i = ic->parm.dss1_io.proc >> 8; i; i++) 
2190          i = i >> 8; /* add one byte */    
2191        l = ic->parm.dss1_io.datalen + proc_len + 8; /* length excluding ie header */
2192        if (l > 255) 
2193          return(-2); /* too long */
2194
2195        if (!(id = new_invoke_id(st))) 
2196          return(0); /* first get a invoke id -> return if no available */
2197        
2198        i = -1; 
2199        MsgHead(p, i, MT_FACILITY); /* build message head */
2200        *p++ = 0x1C; /* Facility IE */
2201        *p++ = l; /* length of ie */
2202        *p++ = 0x91; /* remote operations */
2203        *p++ = 0xA1; /* invoke */
2204        *p++ = l - 3; /* length of invoke */
2205        *p++ = 0x02; /* invoke id tag */
2206        *p++ = 0x01; /* length is 1 */
2207        *p++ = id; /* invoke id */
2208        *p++ = 0x02; /* operation */
2209        *p++ = proc_len; /* length of operation */
2210        
2211        for (i = proc_len; i; i--)
2212          *p++ = (ic->parm.dss1_io.proc >> (i-1)) & 0xFF;
2213        memcpy(p, ic->parm.dss1_io.data, ic->parm.dss1_io.datalen); /* copy data */
2214        l = (p - temp) + ic->parm.dss1_io.datalen; /* total length */         
2215
2216        if (ic->parm.dss1_io.timeout > 0)
2217         if (!(pc = dss1_new_l3_process(st, -1)))
2218           { free_invoke_id(st, id);
2219             return(-2);
2220           } 
2221        pc->prot.dss1.ll_id = ic->parm.dss1_io.ll_id; /* remember id */ 
2222        pc->prot.dss1.proc = ic->parm.dss1_io.proc; /* and procedure */
2223
2224        if (!(skb = l3_alloc_skb(l))) 
2225          { free_invoke_id(st, id);
2226            if (pc) dss1_release_l3_process(pc);
2227            return(-2);
2228          }
2229        memcpy(skb_put(skb, l), temp, l);
2230        
2231        if (pc)
2232         { pc->prot.dss1.invoke_id = id; /* remember id */
2233           L3AddTimer(&pc->timer, ic->parm.dss1_io.timeout, CC_TDSS1_IO | REQUEST);
2234         }
2235        
2236        l3_msg(st, DL_DATA | REQUEST, skb);
2237        ic->parm.dss1_io.hl_id = id; /* return id */
2238        return(0);
2239
2240      case DSS1_CMD_INVOKE_ABORT:
2241        if ((pc = l3dss1_search_dummy_proc(st, ic->parm.dss1_io.hl_id)))
2242         { L3DelTimer(&pc->timer); /* remove timer */
2243           dss1_release_l3_process(pc);
2244           return(0); 
2245         } 
2246        else
2247         { l3_debug(st, "l3dss1_cmd_global abort unknown id");
2248           return(-2);
2249         } 
2250        break;
2251     
2252      default: 
2253        l3_debug(st, "l3dss1_cmd_global unknown cmd 0x%lx", ic->arg);
2254        return(-1);  
2255    } /* switch ic-> arg */
2256   return(-1);
2257 } /* l3dss1_cmd_global */
2258
2259 static void 
2260 l3dss1_io_timer(struct l3_process *pc)
2261 { isdn_ctrl ic;
2262   struct IsdnCardState *cs = pc->st->l1.hardware;
2263
2264   L3DelTimer(&pc->timer); /* remove timer */
2265
2266   ic.driver = cs->myid;
2267   ic.command = ISDN_STAT_PROT;
2268   ic.arg = DSS1_STAT_INVOKE_ERR;
2269   ic.parm.dss1_io.hl_id = pc->prot.dss1.invoke_id;
2270   ic.parm.dss1_io.ll_id = pc->prot.dss1.ll_id;
2271   ic.parm.dss1_io.proc = pc->prot.dss1.proc;
2272   ic.parm.dss1_io.timeout= -1;
2273   ic.parm.dss1_io.datalen = 0;
2274   ic.parm.dss1_io.data = NULL;
2275   free_invoke_id(pc->st, pc->prot.dss1.invoke_id);
2276   pc->prot.dss1.invoke_id = 0; /* reset id */
2277
2278   cs->iif.statcallb(&ic);
2279
2280   dss1_release_l3_process(pc); 
2281 } /* l3dss1_io_timer */
2282
2283 static void
2284 l3dss1_release_ind(struct l3_process *pc, u_char pr, void *arg)
2285 {
2286         u_char *p;
2287         struct sk_buff *skb = arg;
2288         int callState = 0;
2289         p = skb->data;
2290
2291         if ((p = findie(p, skb->len, IE_CALL_STATE, 0))) {
2292                 p++;
2293                 if (1 == *p++)
2294                         callState = *p;
2295         }
2296         if (callState == 0) {
2297                 /* ETS 300-104 7.6.1, 8.6.1, 10.6.1... and 16.1
2298                  * set down layer 3 without sending any message
2299                  */
2300                 pc->st->l3.l3l4(pc->st, CC_RELEASE | INDICATION, pc);
2301                 newl3state(pc, 0);
2302                 dss1_release_l3_process(pc);
2303         } else {
2304                 pc->st->l3.l3l4(pc->st, CC_IGNORE | INDICATION, pc);
2305         }
2306 }
2307
2308 static void
2309 l3dss1_dummy(struct l3_process *pc, u_char pr, void *arg)
2310 {
2311 }
2312
2313 static void
2314 l3dss1_t302(struct l3_process *pc, u_char pr, void *arg)
2315 {
2316         L3DelTimer(&pc->timer);
2317         pc->para.loc = 0;
2318         pc->para.cause = 28; /* invalid number */
2319         l3dss1_disconnect_req(pc, pr, NULL);
2320         pc->st->l3.l3l4(pc->st, CC_SETUP_ERR, pc);
2321 }
2322
2323 static void
2324 l3dss1_t303(struct l3_process *pc, u_char pr, void *arg)
2325 {
2326         if (pc->N303 > 0) {
2327                 pc->N303--;
2328                 L3DelTimer(&pc->timer);
2329                 l3dss1_setup_req(pc, pr, arg);
2330         } else {
2331                 L3DelTimer(&pc->timer);
2332                 l3dss1_message_cause(pc, MT_RELEASE_COMPLETE, 102);
2333                 pc->st->l3.l3l4(pc->st, CC_NOSETUP_RSP, pc);
2334                 dss1_release_l3_process(pc);
2335         }
2336 }
2337
2338 static void
2339 l3dss1_t304(struct l3_process *pc, u_char pr, void *arg)
2340 {
2341         L3DelTimer(&pc->timer);
2342         pc->para.loc = 0;
2343         pc->para.cause = 102;
2344         l3dss1_disconnect_req(pc, pr, NULL);
2345         pc->st->l3.l3l4(pc->st, CC_SETUP_ERR, pc);
2346
2347 }
2348
2349 static void
2350 l3dss1_t305(struct l3_process *pc, u_char pr, void *arg)
2351 {
2352         u_char tmp[16];
2353         u_char *p = tmp;
2354         int l;
2355         struct sk_buff *skb;
2356         u_char cause = 16;
2357
2358         L3DelTimer(&pc->timer);
2359         if (pc->para.cause != NO_CAUSE)
2360                 cause = pc->para.cause;
2361
2362         MsgHead(p, pc->callref, MT_RELEASE);
2363
2364         *p++ = IE_CAUSE;
2365         *p++ = 0x2;
2366         *p++ = 0x80;
2367         *p++ = cause | 0x80;
2368
2369         l = p - tmp;
2370         if (!(skb = l3_alloc_skb(l)))
2371                 return;
2372         memcpy(skb_put(skb, l), tmp, l);
2373         newl3state(pc, 19);
2374         l3_msg(pc->st, DL_DATA | REQUEST, skb);
2375         L3AddTimer(&pc->timer, T308, CC_T308_1);
2376 }
2377
2378 static void
2379 l3dss1_t310(struct l3_process *pc, u_char pr, void *arg)
2380 {
2381         L3DelTimer(&pc->timer);
2382         pc->para.loc = 0;
2383         pc->para.cause = 102;
2384         l3dss1_disconnect_req(pc, pr, NULL);
2385         pc->st->l3.l3l4(pc->st, CC_SETUP_ERR, pc);
2386 }
2387
2388 static void
2389 l3dss1_t313(struct l3_process *pc, u_char pr, void *arg)
2390 {
2391         L3DelTimer(&pc->timer);
2392         pc->para.loc = 0;
2393         pc->para.cause = 102;
2394         l3dss1_disconnect_req(pc, pr, NULL);
2395         pc->st->l3.l3l4(pc->st, CC_CONNECT_ERR, pc);
2396 }
2397
2398 static void
2399 l3dss1_t308_1(struct l3_process *pc, u_char pr, void *arg)
2400 {
2401         newl3state(pc, 19);
2402         L3DelTimer(&pc->timer);
2403         l3dss1_message(pc, MT_RELEASE);
2404         L3AddTimer(&pc->timer, T308, CC_T308_2);
2405 }
2406
2407 static void
2408 l3dss1_t308_2(struct l3_process *pc, u_char pr, void *arg)
2409 {
2410         L3DelTimer(&pc->timer);
2411         pc->st->l3.l3l4(pc->st, CC_RELEASE_ERR, pc);
2412         dss1_release_l3_process(pc);
2413 }
2414
2415 static void
2416 l3dss1_t318(struct l3_process *pc, u_char pr, void *arg)
2417 {
2418         L3DelTimer(&pc->timer);
2419         pc->para.cause = 102;   /* Timer expiry */
2420         pc->para.loc = 0;       /* local */
2421         pc->st->l3.l3l4(pc->st, CC_RESUME_ERR, pc);
2422         newl3state(pc, 19);
2423         l3dss1_message(pc, MT_RELEASE);
2424         L3AddTimer(&pc->timer, T308, CC_T308_1);
2425 }
2426
2427 static void
2428 l3dss1_t319(struct l3_process *pc, u_char pr, void *arg)
2429 {
2430         L3DelTimer(&pc->timer);
2431         pc->para.cause = 102;   /* Timer expiry */
2432         pc->para.loc = 0;       /* local */
2433         pc->st->l3.l3l4(pc->st, CC_SUSPEND_ERR, pc);
2434         newl3state(pc, 10);
2435 }
2436
2437 static void
2438 l3dss1_restart(struct l3_process *pc, u_char pr, void *arg)
2439 {
2440         L3DelTimer(&pc->timer);
2441         pc->st->l3.l3l4(pc->st, CC_RELEASE | INDICATION, pc);
2442         dss1_release_l3_process(pc);
2443 }
2444
2445 static void
2446 l3dss1_status(struct l3_process *pc, u_char pr, void *arg)
2447 {
2448         u_char *p;
2449         struct sk_buff *skb = arg;
2450         int ret; 
2451         u_char cause = 0, callState = 0;
2452         
2453         if ((ret = l3dss1_get_cause(pc, skb))) {
2454                 if (pc->debug & L3_DEB_WARN)
2455                         l3_debug(pc->st, "STATUS get_cause ret(%d)",ret);
2456                 if (ret < 0)
2457                         cause = 96;
2458                 else if (ret > 0)
2459                         cause = 100;
2460         }
2461         if ((p = findie(skb->data, skb->len, IE_CALL_STATE, 0))) {
2462                 p++;
2463                 if (1 == *p++) {
2464                         callState = *p;
2465                         if (!ie_in_set(pc, *p, l3_valid_states))
2466                                 cause = 100;
2467                 } else
2468                         cause = 100;
2469         } else
2470                 cause = 96;
2471         if (!cause) { /*  no error before */
2472                 ret = check_infoelements(pc, skb, ie_STATUS);
2473                 if (ERR_IE_COMPREHENSION == ret)
2474                         cause = 96;
2475                 else if (ERR_IE_UNRECOGNIZED == ret)
2476                         cause = 99;
2477         }
2478         if (cause) {
2479                 u_char tmp;
2480                 
2481                 if (pc->debug & L3_DEB_WARN)
2482                         l3_debug(pc->st, "STATUS error(%d/%d)",ret,cause);
2483                 tmp = pc->para.cause;
2484                 pc->para.cause = cause;
2485                 l3dss1_status_send(pc, 0, NULL);
2486                 if (cause == 99)
2487                         pc->para.cause = tmp;
2488                 else
2489                         return;
2490         }
2491         cause = pc->para.cause;
2492         if (((cause & 0x7f) == 111) && (callState == 0)) {
2493                 /* ETS 300-104 7.6.1, 8.6.1, 10.6.1...
2494                  * if received MT_STATUS with cause == 111 and call
2495                  * state == 0, then we must set down layer 3
2496                  */
2497                 pc->st->l3.l3l4(pc->st, CC_RELEASE | INDICATION, pc);
2498                 newl3state(pc, 0);
2499                 dss1_release_l3_process(pc);
2500         }
2501 }
2502
2503 static void
2504 l3dss1_facility(struct l3_process *pc, u_char pr, void *arg)
2505 {
2506         struct sk_buff *skb = arg;
2507         int ret;
2508         
2509         ret = check_infoelements(pc, skb, ie_FACILITY);
2510         l3dss1_std_ie_err(pc, ret);
2511           {
2512                 u_char *p;
2513                 if ((p = findie(skb->data, skb->len, IE_FACILITY, 0)))
2514                         l3dss1_parse_facility(pc->st, pc, pc->callref, p);
2515         }
2516 }
2517
2518 static void
2519 l3dss1_suspend_req(struct l3_process *pc, u_char pr, void *arg)
2520 {
2521         struct sk_buff *skb;
2522         u_char tmp[32];
2523         u_char *p = tmp;
2524         u_char i, l;
2525         u_char *msg = pc->chan->setup.phone;
2526
2527         MsgHead(p, pc->callref, MT_SUSPEND);
2528         l = *msg++;
2529         if (l && (l <= 10)) {   /* Max length 10 octets */
2530                 *p++ = IE_CALL_ID;
2531                 *p++ = l;
2532                 for (i = 0; i < l; i++)
2533                         *p++ = *msg++;
2534         } else if (l) {
2535                 l3_debug(pc->st, "SUS wrong CALL_ID len %d", l);
2536                 return;
2537         }
2538         l = p - tmp;
2539         if (!(skb = l3_alloc_skb(l)))
2540                 return;
2541         memcpy(skb_put(skb, l), tmp, l);
2542         l3_msg(pc->st, DL_DATA | REQUEST, skb);
2543         newl3state(pc, 15);
2544         L3AddTimer(&pc->timer, T319, CC_T319);
2545 }
2546
2547 static void
2548 l3dss1_suspend_ack(struct l3_process *pc, u_char pr, void *arg)
2549 {
2550         struct sk_buff *skb = arg;
2551         int ret;
2552
2553         L3DelTimer(&pc->timer);
2554         newl3state(pc, 0);
2555         pc->para.cause = NO_CAUSE;
2556         pc->st->l3.l3l4(pc->st, CC_SUSPEND | CONFIRM, pc);
2557         /* We don't handle suspend_ack for IE errors now */
2558         if ((ret = check_infoelements(pc, skb, ie_SUSPEND_ACKNOWLEDGE)))
2559                 if (pc->debug & L3_DEB_WARN)
2560                         l3_debug(pc->st, "SUSPACK check ie(%d)",ret);
2561         dss1_release_l3_process(pc);
2562 }
2563
2564 static void
2565 l3dss1_suspend_rej(struct l3_process *pc, u_char pr, void *arg)
2566 {
2567         struct sk_buff *skb = arg;
2568         int ret;
2569
2570         if ((ret = l3dss1_get_cause(pc, skb))) {
2571                 if (pc->debug & L3_DEB_WARN)
2572                         l3_debug(pc->st, "SUSP_REJ get_cause ret(%d)",ret);
2573                 if (ret < 0) 
2574                         pc->para.cause = 96;
2575                 else
2576                         pc->para.cause = 100;
2577                 l3dss1_status_send(pc, pr, NULL);
2578                 return;
2579         }
2580         ret = check_infoelements(pc, skb, ie_SUSPEND_REJECT);
2581         if (ERR_IE_COMPREHENSION == ret) {
2582                 l3dss1_std_ie_err(pc, ret);
2583                 return;
2584         }
2585         L3DelTimer(&pc->timer);
2586         pc->st->l3.l3l4(pc->st, CC_SUSPEND_ERR, pc);
2587         newl3state(pc, 10);
2588         if (ret) /* STATUS for none mandatory IE errors after actions are taken */
2589                 l3dss1_std_ie_err(pc, ret);
2590 }
2591
2592 static void
2593 l3dss1_resume_req(struct l3_process *pc, u_char pr, void *arg)
2594 {
2595         struct sk_buff *skb;
2596         u_char tmp[32];
2597         u_char *p = tmp;
2598         u_char i, l;
2599         u_char *msg = pc->para.setup.phone;
2600
2601         MsgHead(p, pc->callref, MT_RESUME);
2602
2603         l = *msg++;
2604         if (l && (l <= 10)) {   /* Max length 10 octets */
2605                 *p++ = IE_CALL_ID;
2606                 *p++ = l;
2607                 for (i = 0; i < l; i++)
2608                         *p++ = *msg++;
2609         } else if (l) {
2610                 l3_debug(pc->st, "RES wrong CALL_ID len %d", l);
2611                 return;
2612         }
2613         l = p - tmp;
2614         if (!(skb = l3_alloc_skb(l)))
2615                 return;
2616         memcpy(skb_put(skb, l), tmp, l);
2617         l3_msg(pc->st, DL_DATA | REQUEST, skb);
2618         newl3state(pc, 17);
2619         L3AddTimer(&pc->timer, T318, CC_T318);
2620 }
2621
2622 static void
2623 l3dss1_resume_ack(struct l3_process *pc, u_char pr, void *arg)
2624 {
2625         struct sk_buff *skb = arg;
2626         int id, ret;
2627
2628         if ((id = l3dss1_get_channel_id(pc, skb)) > 0) {
2629                 if ((0 == id) || ((3 == id) && (0x10 == pc->para.moderate))) {
2630                         if (pc->debug & L3_DEB_WARN)
2631                                 l3_debug(pc->st, "resume ack with wrong chid %x", id);
2632                         pc->para.cause = 100;
2633                         l3dss1_status_send(pc, pr, NULL);
2634                         return;
2635                 }
2636                 pc->para.bchannel = id;
2637         } else if (1 == pc->state) {
2638                 if (pc->debug & L3_DEB_WARN)
2639                         l3_debug(pc->st, "resume ack without chid (ret %d)", id);
2640                 pc->para.cause = 96;
2641                 l3dss1_status_send(pc, pr, NULL);
2642                 return;
2643         }
2644         ret = check_infoelements(pc, skb, ie_RESUME_ACKNOWLEDGE);
2645         if (ERR_IE_COMPREHENSION == ret) {
2646                 l3dss1_std_ie_err(pc, ret);
2647                 return;
2648         }
2649         L3DelTimer(&pc->timer);
2650         pc->st->l3.l3l4(pc->st, CC_RESUME | CONFIRM, pc);
2651         newl3state(pc, 10);
2652         if (ret) /* STATUS for none mandatory IE errors after actions are taken */
2653                 l3dss1_std_ie_err(pc, ret);
2654 }
2655
2656 static void
2657 l3dss1_resume_rej(struct l3_process *pc, u_char pr, void *arg)
2658 {
2659         struct sk_buff *skb = arg;
2660         int ret;
2661
2662         if ((ret = l3dss1_get_cause(pc, skb))) {
2663                 if (pc->debug & L3_DEB_WARN)
2664                         l3_debug(pc->st, "RES_REJ get_cause ret(%d)",ret);
2665                 if (ret < 0) 
2666                         pc->para.cause = 96;
2667                 else
2668                         pc->para.cause = 100;
2669                 l3dss1_status_send(pc, pr, NULL);
2670                 return;
2671         }
2672         ret = check_infoelements(pc, skb, ie_RESUME_REJECT);
2673         if (ERR_IE_COMPREHENSION == ret) {
2674                 l3dss1_std_ie_err(pc, ret);
2675                 return;
2676         }
2677         L3DelTimer(&pc->timer);
2678         pc->st->l3.l3l4(pc->st, CC_RESUME_ERR, pc);
2679         newl3state(pc, 0);
2680         if (ret) /* STATUS for none mandatory IE errors after actions are taken */
2681                 l3dss1_std_ie_err(pc, ret);
2682         dss1_release_l3_process(pc);
2683 }
2684
2685 static void
2686 l3dss1_global_restart(struct l3_process *pc, u_char pr, void *arg)
2687 {
2688         u_char tmp[32];
2689         u_char *p;
2690         u_char ri, ch = 0, chan = 0;
2691         int l;
2692         struct sk_buff *skb = arg;
2693         struct l3_process *up;
2694
2695         newl3state(pc, 2);
2696         L3DelTimer(&pc->timer);
2697         p = skb->data;
2698         if ((p = findie(p, skb->len, IE_RESTART_IND, 0))) {
2699                 ri = p[2];
2700                 l3_debug(pc->st, "Restart %x", ri);
2701         } else {
2702                 l3_debug(pc->st, "Restart without restart IE");
2703                 ri = 0x86;
2704         }
2705         p = skb->data;
2706         if ((p = findie(p, skb->len, IE_CHANNEL_ID, 0))) {
2707                 chan = p[2] & 3;
2708                 ch = p[2];
2709                 if (pc->st->l3.debug)
2710                         l3_debug(pc->st, "Restart for channel %d", chan);
2711         }
2712         newl3state(pc, 2);
2713         up = pc->st->l3.proc;
2714         while (up) {
2715                 if ((ri & 7) == 7)
2716                         up->st->lli.l4l3(up->st, CC_RESTART | REQUEST, up);
2717                 else if (up->para.bchannel == chan)
2718                         up->st->lli.l4l3(up->st, CC_RESTART | REQUEST, up);
2719                 up = up->next;
2720         }
2721         p = tmp;
2722         MsgHead(p, pc->callref, MT_RESTART_ACKNOWLEDGE);
2723         if (chan) {
2724                 *p++ = IE_CHANNEL_ID;
2725                 *p++ = 1;
2726                 *p++ = ch | 0x80;
2727         }
2728         *p++ = 0x79;            /* RESTART Ind */
2729         *p++ = 1;
2730         *p++ = ri;
2731         l = p - tmp;
2732         if (!(skb = l3_alloc_skb(l)))
2733                 return;
2734         memcpy(skb_put(skb, l), tmp, l);
2735         newl3state(pc, 0);
2736         l3_msg(pc->st, DL_DATA | REQUEST, skb);
2737 }
2738
2739 static void
2740 l3dss1_dl_reset(struct l3_process *pc, u_char pr, void *arg)
2741 {
2742         pc->para.cause = 0x29;          /* Temporary failure */
2743         pc->para.loc = 0;
2744         l3dss1_disconnect_req(pc, pr, NULL);
2745         pc->st->l3.l3l4(pc->st, CC_SETUP_ERR, pc);
2746 }
2747
2748 static void
2749 l3dss1_dl_release(struct l3_process *pc, u_char pr, void *arg)
2750 {
2751         newl3state(pc, 0);
2752         pc->para.cause = 0x1b;          /* Destination out of order */
2753         pc->para.loc = 0;
2754         pc->st->l3.l3l4(pc->st, CC_RELEASE | INDICATION, pc);
2755         release_l3_process(pc);
2756 }
2757
2758 static void
2759 l3dss1_dl_reestablish(struct l3_process *pc, u_char pr, void *arg)
2760 {
2761         L3DelTimer(&pc->timer);
2762         L3AddTimer(&pc->timer, T309, CC_T309);
2763         l3_msg(pc->st, DL_ESTABLISH | REQUEST, NULL);
2764 }
2765  
2766 static void
2767 l3dss1_dl_reest_status(struct l3_process *pc, u_char pr, void *arg)
2768 {
2769         L3DelTimer(&pc->timer);
2770  
2771         pc->para.cause = 0x1F; /* normal, unspecified */
2772         l3dss1_status_send(pc, 0, NULL);
2773 }
2774
2775 /* *INDENT-OFF* */
2776 static struct stateentry downstatelist[] =
2777 {
2778         {SBIT(0),
2779          CC_SETUP | REQUEST, l3dss1_setup_req},
2780         {SBIT(0),
2781          CC_RESUME | REQUEST, l3dss1_resume_req},
2782         {SBIT(1) | SBIT(2) | SBIT(3) | SBIT(4) | SBIT(6) | SBIT(7) | SBIT(8) | SBIT(9) | SBIT(10) | SBIT(25),
2783          CC_DISCONNECT | REQUEST, l3dss1_disconnect_req},
2784         {SBIT(12),
2785          CC_RELEASE | REQUEST, l3dss1_release_req},
2786         {ALL_STATES,
2787          CC_RESTART | REQUEST, l3dss1_restart},
2788         {SBIT(6) | SBIT(25),
2789          CC_IGNORE | REQUEST, l3dss1_reset},
2790         {SBIT(6) | SBIT(25),
2791          CC_REJECT | REQUEST, l3dss1_reject_req},
2792         {SBIT(6) | SBIT(25),
2793          CC_PROCEED_SEND | REQUEST, l3dss1_proceed_req},
2794         {SBIT(6),
2795          CC_MORE_INFO | REQUEST, l3dss1_setup_ack_req},
2796         {SBIT(25),
2797          CC_MORE_INFO | REQUEST, l3dss1_dummy},
2798         {SBIT(6) | SBIT(9) | SBIT(25),
2799          CC_ALERTING | REQUEST, l3dss1_alert_req},
2800         {SBIT(6) | SBIT(7) | SBIT(9) | SBIT(25),
2801          CC_SETUP | RESPONSE, l3dss1_setup_rsp},
2802         {SBIT(10),
2803          CC_SUSPEND | REQUEST, l3dss1_suspend_req},
2804         {SBIT(7) | SBIT(9) | SBIT(25),
2805          CC_REDIR | REQUEST, l3dss1_redir_req},
2806         {SBIT(6),
2807          CC_REDIR | REQUEST, l3dss1_redir_req_early},
2808         {SBIT(9) | SBIT(25),
2809          CC_DISCONNECT | REQUEST, l3dss1_disconnect_req},
2810         {SBIT(25),
2811          CC_T302, l3dss1_t302},
2812         {SBIT(1),
2813          CC_T303, l3dss1_t303},
2814         {SBIT(2),
2815          CC_T304, l3dss1_t304},
2816         {SBIT(3),
2817          CC_T310, l3dss1_t310},
2818         {SBIT(8),
2819          CC_T313, l3dss1_t313},
2820         {SBIT(11),
2821          CC_T305, l3dss1_t305},
2822         {SBIT(15),
2823          CC_T319, l3dss1_t319},
2824         {SBIT(17),
2825          CC_T318, l3dss1_t318},
2826         {SBIT(19),
2827          CC_T308_1, l3dss1_t308_1},
2828         {SBIT(19),
2829          CC_T308_2, l3dss1_t308_2},
2830         {SBIT(10),
2831          CC_T309, l3dss1_dl_release},
2832 };
2833
2834 #define DOWNSLLEN \
2835         (sizeof(downstatelist) / sizeof(struct stateentry))
2836
2837 static struct stateentry datastatelist[] =
2838 {
2839         {ALL_STATES,
2840          MT_STATUS_ENQUIRY, l3dss1_status_enq},
2841         {ALL_STATES,
2842          MT_FACILITY, l3dss1_facility},
2843         {SBIT(19),
2844          MT_STATUS, l3dss1_release_ind},
2845         {ALL_STATES,
2846          MT_STATUS, l3dss1_status},
2847         {SBIT(0),
2848          MT_SETUP, l3dss1_setup},
2849         {SBIT(6) | SBIT(7) | SBIT(8) | SBIT(9) | SBIT(10) | SBIT(11) | SBIT(12) |
2850          SBIT(15) | SBIT(17) | SBIT(19) | SBIT(25),
2851          MT_SETUP, l3dss1_dummy},
2852         {SBIT(1) | SBIT(2),
2853          MT_CALL_PROCEEDING, l3dss1_call_proc},
2854         {SBIT(1),
2855          MT_SETUP_ACKNOWLEDGE, l3dss1_setup_ack},
2856         {SBIT(2) | SBIT(3),
2857          MT_ALERTING, l3dss1_alerting},
2858         {SBIT(2) | SBIT(3),
2859          MT_PROGRESS, l3dss1_progress},
2860         {SBIT(2) | SBIT(3) | SBIT(4) | SBIT(7) | SBIT(8) | SBIT(9) | SBIT(10) |
2861          SBIT(11) | SBIT(12) | SBIT(15) | SBIT(17) | SBIT(19) | SBIT(25),
2862          MT_INFORMATION, l3dss1_information},
2863         {SBIT(10) | SBIT(11) | SBIT(15),
2864          MT_NOTIFY, l3dss1_notify},
2865         {SBIT(0) | SBIT(1) | SBIT(2) | SBIT(3) | SBIT(4) | SBIT(7) | SBIT(8) | SBIT(10) |
2866          SBIT(11) | SBIT(12) | SBIT(15) | SBIT(17) | SBIT(19) | SBIT(25),
2867          MT_RELEASE_COMPLETE, l3dss1_release_cmpl},
2868         {SBIT(1) | SBIT(2) | SBIT(3) | SBIT(4) | SBIT(7) | SBIT(8) | SBIT(9) | SBIT(10) | SBIT(11) | SBIT(12) | SBIT(15) | SBIT(17) | SBIT(25),
2869          MT_RELEASE, l3dss1_release},
2870         {SBIT(19),  MT_RELEASE, l3dss1_release_ind},
2871         {SBIT(1) | SBIT(2) | SBIT(3) | SBIT(4) | SBIT(7) | SBIT(8) | SBIT(9) | SBIT(10) | SBIT(11) | SBIT(15) | SBIT(17) | SBIT(25),
2872          MT_DISCONNECT, l3dss1_disconnect},
2873         {SBIT(19),
2874          MT_DISCONNECT, l3dss1_dummy},
2875         {SBIT(1) | SBIT(2) | SBIT(3) | SBIT(4),
2876          MT_CONNECT, l3dss1_connect},
2877         {SBIT(8),
2878          MT_CONNECT_ACKNOWLEDGE, l3dss1_connect_ack},
2879         {SBIT(15),
2880          MT_SUSPEND_ACKNOWLEDGE, l3dss1_suspend_ack},
2881         {SBIT(15),
2882          MT_SUSPEND_REJECT, l3dss1_suspend_rej},
2883         {SBIT(17),
2884          MT_RESUME_ACKNOWLEDGE, l3dss1_resume_ack},
2885         {SBIT(17),
2886          MT_RESUME_REJECT, l3dss1_resume_rej},
2887 };
2888
2889 #define DATASLLEN \
2890         (sizeof(datastatelist) / sizeof(struct stateentry))
2891
2892 static struct stateentry globalmes_list[] =
2893 {
2894         {ALL_STATES,
2895          MT_STATUS, l3dss1_status},
2896         {SBIT(0),
2897          MT_RESTART, l3dss1_global_restart},
2898 /*      {SBIT(1),
2899          MT_RESTART_ACKNOWLEDGE, l3dss1_restart_ack},
2900 */
2901 };
2902 #define GLOBALM_LEN \
2903         (sizeof(globalmes_list) / sizeof(struct stateentry))
2904
2905 static struct stateentry manstatelist[] =
2906 {
2907         {SBIT(2),
2908          DL_ESTABLISH | INDICATION, l3dss1_dl_reset},
2909         {SBIT(10),
2910          DL_ESTABLISH | CONFIRM, l3dss1_dl_reest_status},
2911         {SBIT(10),
2912          DL_RELEASE | INDICATION, l3dss1_dl_reestablish},
2913         {ALL_STATES,
2914          DL_RELEASE | INDICATION, l3dss1_dl_release},
2915 };
2916
2917 #define MANSLLEN \
2918         (sizeof(manstatelist) / sizeof(struct stateentry))
2919 /* *INDENT-ON* */
2920
2921
2922 static void
2923 global_handler(struct PStack *st, int mt, struct sk_buff *skb)
2924 {
2925         u_char tmp[16];
2926         u_char *p = tmp;
2927         int l;
2928         int i;
2929         struct l3_process *proc = st->l3.global;
2930
2931         proc->callref = skb->data[2]; /* cr flag */
2932         for (i = 0; i < GLOBALM_LEN; i++)
2933                 if ((mt == globalmes_list[i].primitive) &&
2934                     ((1 << proc->state) & globalmes_list[i].state))
2935                         break;
2936         if (i == GLOBALM_LEN) {
2937                 if (st->l3.debug & L3_DEB_STATE) {
2938                         l3_debug(st, "dss1 global state %d mt %x unhandled",
2939                                 proc->state, mt);
2940                 }
2941                 MsgHead(p, proc->callref, MT_STATUS);
2942                 *p++ = IE_CAUSE;
2943                 *p++ = 0x2;
2944                 *p++ = 0x80;
2945                 *p++ = 81 |0x80;        /* invalid cr */
2946                 *p++ = 0x14;            /* CallState */
2947                 *p++ = 0x1;
2948                 *p++ = proc->state & 0x3f;
2949                 l = p - tmp;
2950                 if (!(skb = l3_alloc_skb(l)))
2951                         return;
2952                 memcpy(skb_put(skb, l), tmp, l);
2953                 l3_msg(proc->st, DL_DATA | REQUEST, skb);
2954         } else {
2955                 if (st->l3.debug & L3_DEB_STATE) {
2956                         l3_debug(st, "dss1 global %d mt %x",
2957                                 proc->state, mt);
2958                 }
2959                 globalmes_list[i].rout(proc, mt, skb);
2960         }
2961 }
2962
2963 static void
2964 dss1up(struct PStack *st, int pr, void *arg)
2965 {
2966         int i, mt, cr, cause, callState;
2967         char *ptr;
2968         u_char *p;
2969         struct sk_buff *skb = arg;
2970         struct l3_process *proc;
2971
2972         switch (pr) {
2973                 case (DL_DATA | INDICATION):
2974                 case (DL_UNIT_DATA | INDICATION):
2975                         break;
2976                 case (DL_ESTABLISH | CONFIRM):
2977                 case (DL_ESTABLISH | INDICATION):
2978                 case (DL_RELEASE | INDICATION):
2979                 case (DL_RELEASE | CONFIRM):
2980                         l3_msg(st, pr, arg);
2981                         return;
2982                         break;
2983                 default:
2984                         printk(KERN_ERR "HiSax dss1up unknown pr=%04x\n", pr);
2985                         return;
2986         }
2987         if (skb->len < 3) {
2988                 l3_debug(st, "dss1up frame too short(%d)", skb->len);
2989                 dev_kfree_skb(skb);
2990                 return;
2991         }
2992
2993         if (skb->data[0] != PROTO_DIS_EURO) {
2994                 if (st->l3.debug & L3_DEB_PROTERR) {
2995                         l3_debug(st, "dss1up%sunexpected discriminator %x message len %d",
2996                                  (pr == (DL_DATA | INDICATION)) ? " " : "(broadcast) ",
2997                                  skb->data[0], skb->len);
2998                 }
2999                 dev_kfree_skb(skb);
3000                 return;
3001         }
3002         cr = getcallref(skb->data);
3003         if (skb->len < ((skb->data[1] & 0x0f) + 3)) {
3004                 l3_debug(st, "dss1up frame too short(%d)", skb->len);
3005                 dev_kfree_skb(skb);
3006                 return;
3007         }
3008         mt = skb->data[skb->data[1] + 2];
3009         if (st->l3.debug & L3_DEB_STATE)
3010                 l3_debug(st, "dss1up cr %d", cr);
3011         if (cr == -2) {  /* wrong Callref */
3012                 if (st->l3.debug & L3_DEB_WARN)
3013                         l3_debug(st, "dss1up wrong Callref");
3014                 dev_kfree_skb(skb);
3015                 return;
3016         } else if (cr == -1) {  /* Dummy Callref */
3017                 if (mt == MT_FACILITY)
3018                         if ((p = findie(skb->data, skb->len, IE_FACILITY, 0))) {
3019                                 l3dss1_parse_facility(st, NULL, 
3020                                         (pr == (DL_DATA | INDICATION)) ? -1 : -2, p); 
3021                                 dev_kfree_skb(skb);
3022                                 return;  
3023                         }
3024                 if (st->l3.debug & L3_DEB_WARN)
3025                         l3_debug(st, "dss1up dummy Callref (no facility msg or ie)");
3026                 dev_kfree_skb(skb);
3027                 return;
3028         } else if ((((skb->data[1] & 0x0f) == 1) && (0==(cr & 0x7f))) ||
3029                 (((skb->data[1] & 0x0f) == 2) && (0==(cr & 0x7fff)))) { /* Global CallRef */
3030                 if (st->l3.debug & L3_DEB_STATE)
3031                         l3_debug(st, "dss1up Global CallRef");
3032                 global_handler(st, mt, skb);
3033                 dev_kfree_skb(skb);
3034                 return;
3035         } else if (!(proc = getl3proc(st, cr))) {
3036                 /* No transaction process exist, that means no call with
3037                  * this callreference is active
3038                  */
3039                 if (mt == MT_SETUP) {
3040                         /* Setup creates a new transaction process */
3041                         if (skb->data[2] & 0x80) {
3042                                 /* Setup with wrong CREF flag */
3043                                 if (st->l3.debug & L3_DEB_STATE)
3044                                         l3_debug(st, "dss1up wrong CRef flag");
3045                                 dev_kfree_skb(skb);
3046                                 return;
3047                         }
3048                         if (!(proc = dss1_new_l3_process(st, cr))) {
3049                                 /* May be to answer with RELEASE_COMPLETE and
3050                                  * CAUSE 0x2f "Resource unavailable", but this
3051                                  * need a new_l3_process too ... arghh
3052                                  */
3053                                 dev_kfree_skb(skb);
3054                                 return;
3055                         }
3056                 } else if (mt == MT_STATUS) {
3057                         cause = 0;
3058                         if ((ptr = findie(skb->data, skb->len, IE_CAUSE, 0)) != NULL) {
3059                                 ptr++;
3060                                 if (*ptr++ == 2)
3061                                         ptr++;
3062                                 cause = *ptr & 0x7f;
3063                         }
3064                         callState = 0;
3065                         if ((ptr = findie(skb->data, skb->len, IE_CALL_STATE, 0)) != NULL) {
3066                                 ptr++;
3067                                 if (*ptr++ == 2)
3068                                         ptr++;
3069                                 callState = *ptr;
3070                         }
3071                         /* ETS 300-104 part 2.4.1
3072                          * if setup has not been made and a message type
3073                          * MT_STATUS is received with call state == 0,
3074                          * we must send nothing
3075                          */
3076                         if (callState != 0) {
3077                                 /* ETS 300-104 part 2.4.2
3078                                  * if setup has not been made and a message type
3079                                  * MT_STATUS is received with call state != 0,
3080                                  * we must send MT_RELEASE_COMPLETE cause 101
3081                                  */
3082                                 if ((proc = dss1_new_l3_process(st, cr))) {
3083                                         proc->para.cause = 101;
3084                                         l3dss1_msg_without_setup(proc, 0, NULL);
3085                                 }
3086                         }
3087                         dev_kfree_skb(skb);
3088                         return;
3089                 } else if (mt == MT_RELEASE_COMPLETE) {
3090                         dev_kfree_skb(skb);
3091                         return;
3092                 } else {
3093                         /* ETS 300-104 part 2
3094                          * if setup has not been made and a message type
3095                          * (except MT_SETUP and RELEASE_COMPLETE) is received,
3096                          * we must send MT_RELEASE_COMPLETE cause 81 */
3097                         dev_kfree_skb(skb);
3098                         if ((proc = dss1_new_l3_process(st, cr))) {
3099                                 proc->para.cause = 81;
3100                                 l3dss1_msg_without_setup(proc, 0, NULL);
3101                         }
3102                         return;
3103                 }
3104         }
3105         if (l3dss1_check_messagetype_validity(proc, mt, skb)) {
3106                 dev_kfree_skb(skb);
3107                 return;
3108         }
3109         if ((p = findie(skb->data, skb->len, IE_DISPLAY, 0)) != NULL) 
3110           l3dss1_deliver_display(proc, pr, p); /* Display IE included */
3111         for (i = 0; i < DATASLLEN; i++)
3112                 if ((mt == datastatelist[i].primitive) &&
3113                     ((1 << proc->state) & datastatelist[i].state))
3114                         break;
3115         if (i == DATASLLEN) {
3116                 if (st->l3.debug & L3_DEB_STATE) {
3117                         l3_debug(st, "dss1up%sstate %d mt %#x unhandled",
3118                                 (pr == (DL_DATA | INDICATION)) ? " " : "(broadcast) ",
3119                                 proc->state, mt);
3120                 }
3121                 if ((MT_RELEASE_COMPLETE != mt) && (MT_RELEASE != mt)) {
3122                         proc->para.cause = 101;
3123                         l3dss1_status_send(proc, pr, skb);
3124                 }
3125         } else {
3126                 if (st->l3.debug & L3_DEB_STATE) {
3127                         l3_debug(st, "dss1up%sstate %d mt %x",
3128                                 (pr == (DL_DATA | INDICATION)) ? " " : "(broadcast) ",
3129                                 proc->state, mt);
3130                 }
3131                 datastatelist[i].rout(proc, pr, skb);
3132         }
3133         dev_kfree_skb(skb);
3134         return;
3135 }
3136
3137 static void
3138 dss1down(struct PStack *st, int pr, void *arg)
3139 {
3140         int i, cr;
3141         struct l3_process *proc;
3142         struct Channel *chan;
3143
3144         if ((DL_ESTABLISH | REQUEST) == pr) {
3145                 l3_msg(st, pr, NULL);
3146                 return;
3147         } else if (((CC_SETUP | REQUEST) == pr) || ((CC_RESUME | REQUEST) == pr)) {
3148                 chan = arg;
3149                 cr = newcallref();
3150                 cr |= 0x80;
3151                 if ((proc = dss1_new_l3_process(st, cr))) {
3152                         proc->chan = chan;
3153                         chan->proc = proc;
3154                         memcpy(&proc->para.setup, &chan->setup, sizeof(setup_parm));
3155                         proc->callref = cr;
3156                 }
3157         } else {
3158                 proc = arg;
3159         }
3160         if (!proc) {
3161                 printk(KERN_ERR "HiSax dss1down without proc pr=%04x\n", pr);
3162                 return;
3163         }
3164
3165         if ( pr == (CC_TDSS1_IO | REQUEST)) {
3166                 l3dss1_io_timer(proc); /* timer expires */ 
3167                 return;
3168         }  
3169
3170         for (i = 0; i < DOWNSLLEN; i++)
3171                 if ((pr == downstatelist[i].primitive) &&
3172                     ((1 << proc->state) & downstatelist[i].state))
3173                         break;
3174         if (i == DOWNSLLEN) {
3175                 if (st->l3.debug & L3_DEB_STATE) {
3176                         l3_debug(st, "dss1down state %d prim %#x unhandled",
3177                                 proc->state, pr);
3178                 }
3179         } else {
3180                 if (st->l3.debug & L3_DEB_STATE) {
3181                         l3_debug(st, "dss1down state %d prim %#x",
3182                                 proc->state, pr);
3183                 }
3184                 downstatelist[i].rout(proc, pr, arg);
3185         }
3186 }
3187
3188 static void
3189 dss1man(struct PStack *st, int pr, void *arg)
3190 {
3191         int i;
3192         struct l3_process *proc = arg;
3193  
3194         if (!proc) {
3195                 printk(KERN_ERR "HiSax dss1man without proc pr=%04x\n", pr);
3196                 return;
3197         }
3198         for (i = 0; i < MANSLLEN; i++)
3199                 if ((pr == manstatelist[i].primitive) &&
3200                     ((1 << proc->state) & manstatelist[i].state))
3201                         break;
3202         if (i == MANSLLEN) {
3203                 if (st->l3.debug & L3_DEB_STATE) {
3204                         l3_debug(st, "cr %d dss1man state %d prim %#x unhandled",
3205                                 proc->callref & 0x7f, proc->state, pr);
3206                 }
3207         } else {
3208                 if (st->l3.debug & L3_DEB_STATE) {
3209                         l3_debug(st, "cr %d dss1man state %d prim %#x",
3210                                 proc->callref & 0x7f, proc->state, pr);
3211                 }
3212                 manstatelist[i].rout(proc, pr, arg);
3213         }
3214 }
3215  
3216 void
3217 setstack_dss1(struct PStack *st)
3218 {
3219         char tmp[64];
3220         int i;
3221
3222         st->lli.l4l3 = dss1down;
3223         st->lli.l4l3_proto = l3dss1_cmd_global;
3224         st->l2.l2l3 = dss1up;
3225         st->l3.l3ml3 = dss1man;
3226         st->l3.N303 = 1;
3227         st->prot.dss1.last_invoke_id = 0;
3228         st->prot.dss1.invoke_used[0] = 1; /* Bit 0 must always be set to 1 */
3229         i = 1;
3230         while (i < 32) 
3231                 st->prot.dss1.invoke_used[i++] = 0;   
3232
3233         if (!(st->l3.global = kmalloc(sizeof(struct l3_process), GFP_ATOMIC))) {
3234                 printk(KERN_ERR "HiSax can't get memory for dss1 global CR\n");
3235         } else {
3236                 st->l3.global->state = 0;
3237                 st->l3.global->callref = 0;
3238                 st->l3.global->next = NULL;
3239                 st->l3.global->debug = L3_DEB_WARN;
3240                 st->l3.global->st = st;
3241                 st->l3.global->N303 = 1;
3242                 st->l3.global->prot.dss1.invoke_id = 0; 
3243
3244                 L3InitTimer(st->l3.global, &st->l3.global->timer);
3245         }
3246         strcpy(tmp, dss1_revision);
3247         printk(KERN_INFO "HiSax: DSS1 Rev. %s\n", HiSax_getrev(tmp));
3248 }