2 * CAPI encoder/decoder for
3 * Portugal Telecom CAPI 2.0
5 * Copyright (C) 1996 Universidade de Lisboa
7 * Written by Pedro Roque Marques (pedro_m@yahoo.com)
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/sched.h>
31 #include <linux/string.h>
32 #include <linux/kernel.h>
34 #include <linux/types.h>
35 #include <linux/slab.h>
38 #include <linux/tqueue.h>
39 #include <linux/skbuff.h>
42 #include <asm/string.h>
44 #include <linux/isdnif.h>
52 * Encoding of CAPI messages
56 int capi_conn_req(const char * calledPN, struct sk_buff **skb, int proto)
69 * CalledPN - 2 + strlen
76 len = 18 + strlen(calledPN);
78 if (proto == ISDN_PROTO_L2_TRANS)
81 if ((*skb = dev_alloc_skb(len)) == NULL) {
83 printk(KERN_WARNING "capi_conn_req: alloc_skb failed\n");
88 *((ushort*) skb_put(*skb, 2)) = AppInfoMask;
90 if (proto == ISDN_PROTO_L2_TRANS)
92 /* Bearer Capability - Mandatory*/
93 *(skb_put(*skb, 1)) = 3; /* BC0.Length */
94 *(skb_put(*skb, 1)) = 0x80; /* Speech */
95 *(skb_put(*skb, 1)) = 0x10; /* Circuit Mode */
96 *(skb_put(*skb, 1)) = 0x23; /* A-law */
100 /* Bearer Capability - Mandatory*/
101 *(skb_put(*skb, 1)) = 2; /* BC0.Length */
102 *(skb_put(*skb, 1)) = 0x88; /* Digital Information */
103 *(skb_put(*skb, 1)) = 0x90; /* BC0.Octect4 */
106 /* Bearer Capability - Optional*/
107 *(skb_put(*skb, 1)) = 0; /* BC1.Length = 0 */
109 *(skb_put(*skb, 1)) = 1; /* ChannelID.Length = 1 */
110 *(skb_put(*skb, 1)) = 0x83; /* Basic Interface - Any Channel */
112 *(skb_put(*skb, 1)) = 0; /* Keypad.Length = 0 */
115 *(skb_put(*skb, 1)) = 0; /* CallingPN.Length = 0 */
116 *(skb_put(*skb, 1)) = 0; /* CallingPSA.Length = 0 */
118 /* Called Party Number */
119 *(skb_put(*skb, 1)) = strlen(calledPN) + 1;
120 *(skb_put(*skb, 1)) = 0x81;
121 memcpy(skb_put(*skb, strlen(calledPN)), calledPN, strlen(calledPN));
125 *(skb_put(*skb, 1)) = 0; /* CalledPSA.Length = 0 */
127 /* LLC.Length = 0; */
128 /* HLC0.Length = 0; */
129 /* HLC1.Length = 0; */
130 /* UTUS.Length = 0; */
131 memset(skb_put(*skb, 4), 0, 4);
136 int capi_conn_resp(struct pcbit_chan* chan, struct sk_buff **skb)
139 if ((*skb = dev_alloc_skb(5)) == NULL) {
141 printk(KERN_WARNING "capi_conn_resp: alloc_skb failed\n");
145 *((ushort*) skb_put(*skb, 2) ) = chan->callref;
146 *(skb_put(*skb, 1)) = 0x01; /* ACCEPT_CALL */
147 *(skb_put(*skb, 1)) = 0;
148 *(skb_put(*skb, 1)) = 0;
153 int capi_conn_active_req(struct pcbit_chan* chan, struct sk_buff **skb)
159 if ((*skb = dev_alloc_skb(8)) == NULL) {
161 printk(KERN_WARNING "capi_conn_active_req: alloc_skb failed\n");
165 *((ushort*) skb_put(*skb, 2) ) = chan->callref;
168 printk(KERN_DEBUG "Call Reference: %04x\n", chan->callref);
171 *(skb_put(*skb, 1)) = 0; /* BC.Length = 0; */
172 *(skb_put(*skb, 1)) = 0; /* ConnectedPN.Length = 0 */
173 *(skb_put(*skb, 1)) = 0; /* PSA.Length */
174 *(skb_put(*skb, 1)) = 0; /* LLC.Length = 0; */
175 *(skb_put(*skb, 1)) = 0; /* HLC.Length = 0; */
176 *(skb_put(*skb, 1)) = 0; /* UTUS.Length = 0; */
181 int capi_conn_active_resp(struct pcbit_chan* chan, struct sk_buff **skb)
187 if ((*skb = dev_alloc_skb(2)) == NULL) {
189 printk(KERN_WARNING "capi_conn_active_resp: alloc_skb failed\n");
193 *((ushort*) skb_put(*skb, 2) ) = chan->callref;
199 int capi_select_proto_req(struct pcbit_chan *chan, struct sk_buff **skb,
207 if ((*skb = dev_alloc_skb(18)) == NULL) {
209 printk(KERN_WARNING "capi_select_proto_req: alloc_skb failed\n");
213 *((ushort*) skb_put(*skb, 2) ) = chan->callref;
215 /* Layer2 protocol */
217 switch (chan->proto) {
218 case ISDN_PROTO_L2_X75I:
219 *(skb_put(*skb, 1)) = 0x05; /* LAPB */
221 case ISDN_PROTO_L2_HDLC:
222 *(skb_put(*skb, 1)) = 0x02;
224 case ISDN_PROTO_L2_TRANS:
228 *(skb_put(*skb, 1)) = 0x06;
232 printk(KERN_DEBUG "Transparent\n");
234 *(skb_put(*skb, 1)) = 0x03;
238 *(skb_put(*skb, 1)) = (outgoing ? 0x02 : 0x42); /* Don't ask */
239 *(skb_put(*skb, 1)) = 0x00;
241 *((ushort *) skb_put(*skb, 2)) = MRU;
244 *(skb_put(*skb, 1)) = 0x08; /* Modulo */
245 *(skb_put(*skb, 1)) = 0x07; /* Max Window */
247 *(skb_put(*skb, 1)) = 0x01; /* No Layer3 Protocol */
250 * 2 - layer3 MTU [10]
253 * - layer1 proto [14]
256 * - layer1dataformat [17]
259 memset(skb_put(*skb, 8), 0, 8);
265 int capi_activate_transp_req(struct pcbit_chan *chan, struct sk_buff **skb)
268 if ((*skb = dev_alloc_skb(7)) == NULL) {
270 printk(KERN_WARNING "capi_activate_transp_req: alloc_skb failed\n");
274 *((ushort*) skb_put(*skb, 2) ) = chan->callref;
277 *(skb_put(*skb, 1)) = chan->layer2link; /* Layer2 id */
278 *(skb_put(*skb, 1)) = 0x00; /* Transmit by default */
280 *((ushort *) skb_put(*skb, 2)) = MRU;
282 *(skb_put(*skb, 1)) = 0x01; /* Enables reception*/
287 int capi_tdata_req(struct pcbit_chan* chan, struct sk_buff *skb)
302 if(skb_headroom(skb) < 10)
304 printk(KERN_CRIT "No headspace (%u) on headroom %p for capi header\n", skb_headroom(skb), skb);
311 *((u16 *) (skb->data)) = chan->callref;
312 skb->data[2] = chan->layer2link;
313 *((u16 *) (skb->data + 3)) = data_len;
315 chan->s_refnum = (chan->s_refnum + 1) % 8;
316 *((u32 *) (skb->data + 5)) = chan->s_refnum;
318 skb->data[9] = 0; /* HDLC frame number */
323 int capi_tdata_resp(struct pcbit_chan *chan, struct sk_buff ** skb)
326 if ((*skb = dev_alloc_skb(4)) == NULL) {
328 printk(KERN_WARNING "capi_tdata_resp: alloc_skb failed\n");
332 *((ushort*) skb_put(*skb, 2) ) = chan->callref;
334 *(skb_put(*skb, 1)) = chan->layer2link;
335 *(skb_put(*skb, 1)) = chan->r_refnum;
340 int capi_disc_req(ushort callref, struct sk_buff **skb, u_char cause)
343 if ((*skb = dev_alloc_skb(6)) == NULL) {
345 printk(KERN_WARNING "capi_disc_req: alloc_skb failed\n");
349 *((ushort*) skb_put(*skb, 2) ) = callref;
351 *(skb_put(*skb, 1)) = 2; /* Cause.Length = 2; */
352 *(skb_put(*skb, 1)) = 0x80;
353 *(skb_put(*skb, 1)) = 0x80 | cause;
356 * Change it: we should send 'Sic transit gloria Mundi' here ;-)
359 *(skb_put(*skb, 1)) = 0; /* UTUS.Length = 0; */
364 int capi_disc_resp(struct pcbit_chan *chan, struct sk_buff **skb)
366 if ((*skb = dev_alloc_skb(2)) == NULL) {
368 printk(KERN_WARNING "capi_disc_resp: alloc_skb failed\n");
372 *((ushort*) skb_put(*skb, 2)) = chan->callref;
379 * Decoding of CAPI messages
383 int capi_decode_conn_ind(struct pcbit_chan * chan,
385 struct callb_data *info)
389 /* Call Reference [CAPI] */
390 chan->callref = *((ushort*) skb->data);
394 printk(KERN_DEBUG "Call Reference: %04x\n", chan->callref);
397 /* Channel Identification */
401 Octect 3 = 0100 10CC - [ 7 Basic, 4 , 2-1 chan ]
404 CIlen = skb->data[0];
408 if ( ((skb->data[1]) & 0xFC) == 0x48 )
409 printk(KERN_DEBUG "decode_conn_ind: chan ok\n");
410 printk(KERN_DEBUG "phyChan = %d\n", skb->data[1] & 0x03);
413 printk(KERN_DEBUG "conn_ind: CIlen = %d\n", CIlen);
415 skb_pull(skb, CIlen + 1);
417 /* Calling Party Number */
418 /* An "additional service" as far as Portugal Telecom is concerned */
426 printk(KERN_DEBUG "CPN: Octect 3 %02x\n", skb->data[1]);
428 if ((skb->data[1] & 0x80) == 0)
431 if (!(info->data.setup.CallingPN = kmalloc(len - count + 1, GFP_ATOMIC)))
434 memcpy(info->data.setup.CallingPN, skb->data + count + 1,
436 info->data.setup.CallingPN[len - count] = 0;
440 info->data.setup.CallingPN = NULL;
441 printk(KERN_DEBUG "NULL CallingPN\n");
444 skb_pull(skb, len + 1);
446 /* Calling Party Subaddress */
447 skb_pull(skb, skb->data[0] + 1);
449 /* Called Party Number */
456 if ((skb->data[1] & 0x80) == 0)
459 if (!(info->data.setup.CalledPN = kmalloc(len - count + 1, GFP_ATOMIC)))
462 memcpy(info->data.setup.CalledPN, skb->data + count + 1,
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 memcpy(str, skb->data + 2, 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,
631 int capi_decode_disc_conf(struct pcbit_chan *chan, struct sk_buff *skb)
635 errcode = *((ushort*) skb->data);
642 int capi_decode_debug_188(u_char *hdr, ushort hdrlen)
649 if (len < 64 && len == hdrlen - 1) {
650 memcpy(str, hdr + 1, hdrlen - 1);
655 printk("debug message incorrect\n");