2  * CAPI encoder/decoder for
 
   3  * Portugal Telecom CAPI 2.0
 
   5  * Copyright (C) 1996 Universidade de Lisboa
 
   7  * Written by Pedro Roque Marques (roque@di.fc.ul.pt)
 
   9  * This software may be used and distributed according to the terms of 
 
  10  * the GNU General Public License, incorporated herein by reference.
 
  12  * Not compatible with the AVM Gmbh. CAPI 2.0
 
  18  *        - "Common ISDN API - Perfil Português - Versão 2.1",
 
  19  *           Telecom Portugal, Fev 1992.
 
  20  *        - "Common ISDN API - Especificação de protocolos para
 
  21  *           acesso aos canais B", Inesc, Jan 1994.
 
  25  *        TODO: better decoding of Information Elements
 
  26  *              for debug purposes mainly
 
  27  *              encode our number in CallerPN and ConnectedPN
 
  30 #include <linux/string.h>
 
  31 #include <linux/kernel.h>
 
  33 #include <linux/types.h>
 
  34 #include <linux/slab.h>
 
  37 #include <linux/skbuff.h>
 
  40 #include <asm/string.h>
 
  42 #include <linux/isdnif.h>
 
  50  *  Encoding of CAPI messages
 
  54 int capi_conn_req(const char * calledPN, struct sk_buff **skb, int proto)
 
  67          *   CalledPN    - 2 + strlen
 
  74         len = 18 + strlen(calledPN);
 
  76         if (proto == ISDN_PROTO_L2_TRANS)
 
  79         if ((*skb = dev_alloc_skb(len)) == NULL) {
 
  81                 printk(KERN_WARNING "capi_conn_req: alloc_skb failed\n");
 
  86         *((ushort*) skb_put(*skb, 2)) = AppInfoMask; 
 
  88         if (proto == ISDN_PROTO_L2_TRANS)
 
  90                 /* Bearer Capability - Mandatory*/
 
  91                 *(skb_put(*skb, 1)) = 3;        /* BC0.Length           */
 
  92                 *(skb_put(*skb, 1)) = 0x80;     /* Speech               */
 
  93                 *(skb_put(*skb, 1)) = 0x10;     /* Circuit Mode         */
 
  94                 *(skb_put(*skb, 1)) = 0x23;     /* A-law                */
 
  98                 /* Bearer Capability - Mandatory*/
 
  99                 *(skb_put(*skb, 1)) = 2;        /* BC0.Length           */
 
 100                 *(skb_put(*skb, 1)) = 0x88;     /* Digital Information  */
 
 101                 *(skb_put(*skb, 1)) = 0x90;     /* BC0.Octect4          */
 
 104         /* Bearer Capability - Optional*/
 
 105         *(skb_put(*skb, 1)) = 0;        /* BC1.Length = 0                    */
 
 107         *(skb_put(*skb, 1)) = 1;        /* ChannelID.Length = 1              */
 
 108         *(skb_put(*skb, 1)) = 0x83;     /* Basic Interface - Any Channel     */
 
 110         *(skb_put(*skb, 1)) = 0;        /* Keypad.Length = 0                 */
 
 113         *(skb_put(*skb, 1)) = 0;        /* CallingPN.Length = 0              */
 
 114         *(skb_put(*skb, 1)) = 0;        /* CallingPSA.Length = 0             */
 
 116         /* Called Party Number */
 
 117         *(skb_put(*skb, 1)) = strlen(calledPN) + 1;
 
 118         *(skb_put(*skb, 1)) = 0x81;
 
 119         memcpy(skb_put(*skb, strlen(calledPN)), calledPN, strlen(calledPN));
 
 123         *(skb_put(*skb, 1)) = 0;       /* CalledPSA.Length = 0     */
 
 125         /* LLC.Length  = 0; */
 
 126         /* HLC0.Length = 0; */
 
 127         /* HLC1.Length = 0; */ 
 
 128         /* UTUS.Length = 0; */
 
 129         memset(skb_put(*skb, 4), 0, 4);
 
 134 int capi_conn_resp(struct pcbit_chan* chan, struct sk_buff **skb)
 
 137         if ((*skb = dev_alloc_skb(5)) == NULL) {
 
 139                 printk(KERN_WARNING "capi_conn_resp: alloc_skb failed\n");
 
 143         *((ushort*) skb_put(*skb, 2) ) = chan->callref;  
 
 144         *(skb_put(*skb, 1)) = 0x01;  /* ACCEPT_CALL */
 
 145         *(skb_put(*skb, 1)) = 0;
 
 146         *(skb_put(*skb, 1)) = 0;
 
 151 int capi_conn_active_req(struct pcbit_chan* chan, struct sk_buff **skb)
 
 157         if ((*skb = dev_alloc_skb(8)) == NULL) {
 
 159                 printk(KERN_WARNING "capi_conn_active_req: alloc_skb failed\n");
 
 163         *((ushort*) skb_put(*skb, 2) ) = chan->callref;  
 
 166         printk(KERN_DEBUG "Call Reference: %04x\n", chan->callref); 
 
 169         *(skb_put(*skb, 1)) = 0;       /*  BC.Length = 0;          */
 
 170         *(skb_put(*skb, 1)) = 0;       /*  ConnectedPN.Length = 0  */
 
 171         *(skb_put(*skb, 1)) = 0;       /*  PSA.Length              */
 
 172         *(skb_put(*skb, 1)) = 0;       /*  LLC.Length = 0;         */
 
 173         *(skb_put(*skb, 1)) = 0;       /*  HLC.Length = 0;         */
 
 174         *(skb_put(*skb, 1)) = 0;       /*  UTUS.Length = 0;        */
 
 179 int capi_conn_active_resp(struct pcbit_chan* chan, struct sk_buff **skb)
 
 185         if ((*skb = dev_alloc_skb(2)) == NULL) {
 
 187                 printk(KERN_WARNING "capi_conn_active_resp: alloc_skb failed\n");
 
 191         *((ushort*) skb_put(*skb, 2) ) = chan->callref;  
 
 197 int capi_select_proto_req(struct pcbit_chan *chan, struct sk_buff **skb, 
 
 205         if ((*skb = dev_alloc_skb(18)) == NULL) {
 
 207                 printk(KERN_WARNING "capi_select_proto_req: alloc_skb failed\n");
 
 211         *((ushort*) skb_put(*skb, 2) ) = chan->callref;  
 
 213         /* Layer2 protocol */
 
 215         switch (chan->proto) {
 
 216         case ISDN_PROTO_L2_X75I: 
 
 217                 *(skb_put(*skb, 1)) = 0x05;            /* LAPB */
 
 219         case ISDN_PROTO_L2_HDLC:
 
 220                 *(skb_put(*skb, 1)) = 0x02;
 
 222         case ISDN_PROTO_L2_TRANS:
 
 226                 *(skb_put(*skb, 1)) = 0x06;
 
 230                 printk(KERN_DEBUG "Transparent\n");
 
 232                 *(skb_put(*skb, 1)) = 0x03;
 
 236         *(skb_put(*skb, 1)) = (outgoing ? 0x02 : 0x42);    /* Don't ask */
 
 237         *(skb_put(*skb, 1)) = 0x00;
 
 239         *((ushort *) skb_put(*skb, 2)) = MRU;
 
 242         *(skb_put(*skb, 1)) = 0x08;           /* Modulo */
 
 243         *(skb_put(*skb, 1)) = 0x07;           /* Max Window */
 
 245         *(skb_put(*skb, 1)) = 0x01;           /* No Layer3 Protocol */
 
 248          * 2 - layer3 MTU       [10]
 
 251          *   - layer1 proto     [14]
 
 254          *   - layer1dataformat [17]
 
 257         memset(skb_put(*skb, 8), 0, 8);
 
 263 int capi_activate_transp_req(struct pcbit_chan *chan, struct sk_buff **skb)
 
 266         if ((*skb = dev_alloc_skb(7)) == NULL) {
 
 268                 printk(KERN_WARNING "capi_activate_transp_req: alloc_skb failed\n");
 
 272         *((ushort*) skb_put(*skb, 2) ) = chan->callref;  
 
 275         *(skb_put(*skb, 1)) = chan->layer2link; /* Layer2 id */
 
 276         *(skb_put(*skb, 1)) = 0x00;             /* Transmit by default */
 
 278         *((ushort *) skb_put(*skb, 2)) = MRU;
 
 280         *(skb_put(*skb, 1)) = 0x01;             /* Enables reception*/
 
 285 int capi_tdata_req(struct pcbit_chan* chan, struct sk_buff *skb)
 
 300         if(skb_headroom(skb) < 10)
 
 302                 printk(KERN_CRIT "No headspace (%u) on headroom %p for capi header\n", skb_headroom(skb), skb);
 
 309         *((u16 *) (skb->data)) = chan->callref;
 
 310         skb->data[2] = chan->layer2link;
 
 311         *((u16 *) (skb->data + 3)) = data_len;
 
 313         chan->s_refnum = (chan->s_refnum + 1) % 8;
 
 314         *((u32 *) (skb->data + 5)) = chan->s_refnum;
 
 316         skb->data[9] = 0;                           /* HDLC frame number */
 
 321 int capi_tdata_resp(struct pcbit_chan *chan, struct sk_buff ** skb)
 
 324         if ((*skb = dev_alloc_skb(4)) == NULL) {
 
 326                 printk(KERN_WARNING "capi_tdata_resp: alloc_skb failed\n");
 
 330         *((ushort*) skb_put(*skb, 2) ) = chan->callref;  
 
 332         *(skb_put(*skb, 1)) = chan->layer2link;
 
 333         *(skb_put(*skb, 1)) = chan->r_refnum;
 
 338 int capi_disc_req(ushort callref, struct sk_buff **skb, u_char cause)
 
 341         if ((*skb = dev_alloc_skb(6)) == NULL) {
 
 343                 printk(KERN_WARNING "capi_disc_req: alloc_skb failed\n");
 
 347         *((ushort*) skb_put(*skb, 2) ) = callref;  
 
 349         *(skb_put(*skb, 1)) = 2;                  /* Cause.Length = 2; */
 
 350         *(skb_put(*skb, 1)) = 0x80;
 
 351         *(skb_put(*skb, 1)) = 0x80 | cause;           
 
 354          * Change it: we should send 'Sic transit gloria Mundi' here ;-) 
 
 357         *(skb_put(*skb, 1)) = 0;                   /* UTUS.Length = 0;  */
 
 362 int capi_disc_resp(struct pcbit_chan *chan, struct sk_buff **skb)
 
 364         if ((*skb = dev_alloc_skb(2)) == NULL) {
 
 366                 printk(KERN_WARNING "capi_disc_resp: alloc_skb failed\n");
 
 370         *((ushort*) skb_put(*skb, 2)) = chan->callref;  
 
 377  *  Decoding of CAPI messages
 
 381 int capi_decode_conn_ind(struct pcbit_chan * chan, 
 
 383                          struct callb_data *info) 
 
 387         /* Call Reference [CAPI] */
 
 388         chan->callref = *((ushort*) skb->data);
 
 392         printk(KERN_DEBUG "Call Reference: %04x\n", chan->callref); 
 
 395         /* Channel Identification */
 
 399            Octect 3 = 0100 10CC - [ 7 Basic, 4 , 2-1 chan ]
 
 402         CIlen = skb->data[0];
 
 406                 if ( ((skb->data[1]) & 0xFC) == 0x48 )
 
 407                         printk(KERN_DEBUG "decode_conn_ind: chan ok\n");
 
 408                 printk(KERN_DEBUG "phyChan = %d\n", skb->data[1] & 0x03); 
 
 411                 printk(KERN_DEBUG "conn_ind: CIlen = %d\n", CIlen);
 
 413         skb_pull(skb, CIlen + 1);
 
 415         /* Calling Party Number */
 
 416         /* An "additional service" as far as Portugal Telecom is concerned */
 
 424                 printk(KERN_DEBUG "CPN: Octect 3 %02x\n", skb->data[1]);
 
 426                 if ((skb->data[1] & 0x80) == 0)
 
 429                 if (!(info->data.setup.CallingPN = kmalloc(len - count + 1, GFP_ATOMIC)))
 
 432                 skb_copy_from_linear_data_offset(skb, count + 1,
 
 433                                                  info->data.setup.CallingPN,
 
 435                 info->data.setup.CallingPN[len - count] = 0;
 
 439                 info->data.setup.CallingPN = NULL;
 
 440                 printk(KERN_DEBUG "NULL CallingPN\n");
 
 443         skb_pull(skb, len + 1);
 
 445         /* Calling Party Subaddress */
 
 446         skb_pull(skb, skb->data[0] + 1);
 
 448         /* Called Party Number */
 
 455                 if ((skb->data[1] & 0x80) == 0)
 
 458                 if (!(info->data.setup.CalledPN = kmalloc(len - count + 1, GFP_ATOMIC)))
 
 461                 skb_copy_from_linear_data_offset(skb, count + 1,
 
 462                                                  info->data.setup.CalledPN,
 
 464                 info->data.setup.CalledPN[len - count] = 0;
 
 468                 info->data.setup.CalledPN = NULL;
 
 469                 printk(KERN_DEBUG "NULL CalledPN\n");
 
 472         skb_pull(skb, len + 1);
 
 474         /* Called Party Subaddress */
 
 475         skb_pull(skb, skb->data[0] + 1);
 
 478         skb_pull(skb, skb->data[0] + 1);
 
 481         skb_pull(skb, skb->data[0] + 1);
 
 484         skb_pull(skb, skb->data[0] + 1);
 
 493 int capi_decode_conn_conf(struct pcbit_chan * chan, struct sk_buff *skb,
 
 498         chan->callref = *((ushort *) skb->data);     /* Update CallReference */
 
 501         errcode = *((ushort *) skb->data);   /* read errcode */
 
 504         *complete = *(skb->data);
 
 508         /* This is actually a firmware bug */
 
 511                 printk(KERN_DEBUG "complete=%02x\n", *complete);
 
 516         /* Optional Bearer Capability */
 
 517         skb_pull(skb, *(skb->data) + 1);
 
 519         /* Channel Identification */
 
 520         skb_pull(skb, *(skb->data) + 1);
 
 522         /* High Layer Compatibility follows */
 
 523         skb_pull(skb, *(skb->data) + 1);
 
 528 int capi_decode_conn_actv_ind(struct pcbit_chan * chan, struct sk_buff *skb)
 
 535         /* Yet Another Bearer Capability */
 
 536         skb_pull(skb, *(skb->data) + 1);
 
 539         /* Connected Party Number */
 
 543         if (len > 1 && len < 31) {
 
 544                 skb_copy_from_linear_data_offset(skb, 2, str, len - 1);
 
 546                 printk(KERN_DEBUG "Connected Party Number: %s\n", str);
 
 549                 printk(KERN_DEBUG "actv_ind CPN len = %d\n", len);
 
 552         skb_pull(skb, len + 1);
 
 554         /* Connected Subaddress */
 
 555         skb_pull(skb, *(skb->data) + 1);
 
 557         /* Low Layer Capability */
 
 558         skb_pull(skb, *(skb->data) + 1);
 
 560         /* High Layer Capability */
 
 561         skb_pull(skb, *(skb->data) + 1);
 
 566 int capi_decode_conn_actv_conf(struct pcbit_chan * chan, struct sk_buff *skb)
 
 570         errcode = *((ushort*) skb->data);
 
 573         /* Channel Identification 
 
 574         skb_pull(skb, skb->data[0] + 1);
 
 580 int capi_decode_sel_proto_conf(struct pcbit_chan *chan, struct sk_buff *skb)
 
 584         chan->layer2link = *(skb->data);
 
 587         errcode = *((ushort*) skb->data);
 
 593 int capi_decode_actv_trans_conf(struct pcbit_chan *chan, struct sk_buff *skb)
 
 597         if (chan->layer2link != *(skb->data) )
 
 598                 printk("capi_decode_actv_trans_conf: layer2link doesn't match\n");
 
 602         errcode = *((ushort*) skb->data);
 
 608 int capi_decode_disc_ind(struct pcbit_chan *chan, struct sk_buff *skb)
 
 621         for (i=0; i<len; i++)
 
 622                 printk(KERN_DEBUG "Cause Octect %d: %02x\n", i+3, 
 
 632 int capi_decode_debug_188(u_char *hdr, ushort hdrlen)
 
 639         if (len < 64 && len == hdrlen - 1) {        
 
 640                 memcpy(str, hdr + 1, hdrlen - 1);
 
 645                 printk("debug message incorrect\n");