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/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/skbuff.h>
41 #include <asm/string.h>
43 #include <linux/isdnif.h>
51 * Encoding of CAPI messages
55 int capi_conn_req(const char * calledPN
, struct sk_buff
**skb
, int proto
)
68 * CalledPN - 2 + strlen
75 len
= 18 + strlen(calledPN
);
77 if (proto
== ISDN_PROTO_L2_TRANS
)
80 if ((*skb
= dev_alloc_skb(len
)) == NULL
) {
82 printk(KERN_WARNING
"capi_conn_req: alloc_skb failed\n");
87 *((ushort
*) skb_put(*skb
, 2)) = AppInfoMask
;
89 if (proto
== ISDN_PROTO_L2_TRANS
)
91 /* Bearer Capability - Mandatory*/
92 *(skb_put(*skb
, 1)) = 3; /* BC0.Length */
93 *(skb_put(*skb
, 1)) = 0x80; /* Speech */
94 *(skb_put(*skb
, 1)) = 0x10; /* Circuit Mode */
95 *(skb_put(*skb
, 1)) = 0x23; /* A-law */
99 /* Bearer Capability - Mandatory*/
100 *(skb_put(*skb
, 1)) = 2; /* BC0.Length */
101 *(skb_put(*skb
, 1)) = 0x88; /* Digital Information */
102 *(skb_put(*skb
, 1)) = 0x90; /* BC0.Octect4 */
105 /* Bearer Capability - Optional*/
106 *(skb_put(*skb
, 1)) = 0; /* BC1.Length = 0 */
108 *(skb_put(*skb
, 1)) = 1; /* ChannelID.Length = 1 */
109 *(skb_put(*skb
, 1)) = 0x83; /* Basic Interface - Any Channel */
111 *(skb_put(*skb
, 1)) = 0; /* Keypad.Length = 0 */
114 *(skb_put(*skb
, 1)) = 0; /* CallingPN.Length = 0 */
115 *(skb_put(*skb
, 1)) = 0; /* CallingPSA.Length = 0 */
117 /* Called Party Number */
118 *(skb_put(*skb
, 1)) = strlen(calledPN
) + 1;
119 *(skb_put(*skb
, 1)) = 0x81;
120 memcpy(skb_put(*skb
, strlen(calledPN
)), calledPN
, strlen(calledPN
));
124 *(skb_put(*skb
, 1)) = 0; /* CalledPSA.Length = 0 */
126 /* LLC.Length = 0; */
127 /* HLC0.Length = 0; */
128 /* HLC1.Length = 0; */
129 /* UTUS.Length = 0; */
130 memset(skb_put(*skb
, 4), 0, 4);
135 int capi_conn_resp(struct pcbit_chan
* chan
, struct sk_buff
**skb
)
138 if ((*skb
= dev_alloc_skb(5)) == NULL
) {
140 printk(KERN_WARNING
"capi_conn_resp: alloc_skb failed\n");
144 *((ushort
*) skb_put(*skb
, 2) ) = chan
->callref
;
145 *(skb_put(*skb
, 1)) = 0x01; /* ACCEPT_CALL */
146 *(skb_put(*skb
, 1)) = 0;
147 *(skb_put(*skb
, 1)) = 0;
152 int capi_conn_active_req(struct pcbit_chan
* chan
, struct sk_buff
**skb
)
158 if ((*skb
= dev_alloc_skb(8)) == NULL
) {
160 printk(KERN_WARNING
"capi_conn_active_req: alloc_skb failed\n");
164 *((ushort
*) skb_put(*skb
, 2) ) = chan
->callref
;
167 printk(KERN_DEBUG
"Call Reference: %04x\n", chan
->callref
);
170 *(skb_put(*skb
, 1)) = 0; /* BC.Length = 0; */
171 *(skb_put(*skb
, 1)) = 0; /* ConnectedPN.Length = 0 */
172 *(skb_put(*skb
, 1)) = 0; /* PSA.Length */
173 *(skb_put(*skb
, 1)) = 0; /* LLC.Length = 0; */
174 *(skb_put(*skb
, 1)) = 0; /* HLC.Length = 0; */
175 *(skb_put(*skb
, 1)) = 0; /* UTUS.Length = 0; */
180 int capi_conn_active_resp(struct pcbit_chan
* chan
, struct sk_buff
**skb
)
186 if ((*skb
= dev_alloc_skb(2)) == NULL
) {
188 printk(KERN_WARNING
"capi_conn_active_resp: alloc_skb failed\n");
192 *((ushort
*) skb_put(*skb
, 2) ) = chan
->callref
;
198 int capi_select_proto_req(struct pcbit_chan
*chan
, struct sk_buff
**skb
,
206 if ((*skb
= dev_alloc_skb(18)) == NULL
) {
208 printk(KERN_WARNING
"capi_select_proto_req: alloc_skb failed\n");
212 *((ushort
*) skb_put(*skb
, 2) ) = chan
->callref
;
214 /* Layer2 protocol */
216 switch (chan
->proto
) {
217 case ISDN_PROTO_L2_X75I
:
218 *(skb_put(*skb
, 1)) = 0x05; /* LAPB */
220 case ISDN_PROTO_L2_HDLC
:
221 *(skb_put(*skb
, 1)) = 0x02;
223 case ISDN_PROTO_L2_TRANS
:
227 *(skb_put(*skb
, 1)) = 0x06;
231 printk(KERN_DEBUG
"Transparent\n");
233 *(skb_put(*skb
, 1)) = 0x03;
237 *(skb_put(*skb
, 1)) = (outgoing
? 0x02 : 0x42); /* Don't ask */
238 *(skb_put(*skb
, 1)) = 0x00;
240 *((ushort
*) skb_put(*skb
, 2)) = MRU
;
243 *(skb_put(*skb
, 1)) = 0x08; /* Modulo */
244 *(skb_put(*skb
, 1)) = 0x07; /* Max Window */
246 *(skb_put(*skb
, 1)) = 0x01; /* No Layer3 Protocol */
249 * 2 - layer3 MTU [10]
252 * - layer1 proto [14]
255 * - layer1dataformat [17]
258 memset(skb_put(*skb
, 8), 0, 8);
264 int capi_activate_transp_req(struct pcbit_chan
*chan
, struct sk_buff
**skb
)
267 if ((*skb
= dev_alloc_skb(7)) == NULL
) {
269 printk(KERN_WARNING
"capi_activate_transp_req: alloc_skb failed\n");
273 *((ushort
*) skb_put(*skb
, 2) ) = chan
->callref
;
276 *(skb_put(*skb
, 1)) = chan
->layer2link
; /* Layer2 id */
277 *(skb_put(*skb
, 1)) = 0x00; /* Transmit by default */
279 *((ushort
*) skb_put(*skb
, 2)) = MRU
;
281 *(skb_put(*skb
, 1)) = 0x01; /* Enables reception*/
286 int capi_tdata_req(struct pcbit_chan
* chan
, struct sk_buff
*skb
)
301 if(skb_headroom(skb
) < 10)
303 printk(KERN_CRIT
"No headspace (%u) on headroom %p for capi header\n", skb_headroom(skb
), skb
);
310 *((u16
*) (skb
->data
)) = chan
->callref
;
311 skb
->data
[2] = chan
->layer2link
;
312 *((u16
*) (skb
->data
+ 3)) = data_len
;
314 chan
->s_refnum
= (chan
->s_refnum
+ 1) % 8;
315 *((u32
*) (skb
->data
+ 5)) = chan
->s_refnum
;
317 skb
->data
[9] = 0; /* HDLC frame number */
322 int capi_tdata_resp(struct pcbit_chan
*chan
, struct sk_buff
** skb
)
325 if ((*skb
= dev_alloc_skb(4)) == NULL
) {
327 printk(KERN_WARNING
"capi_tdata_resp: alloc_skb failed\n");
331 *((ushort
*) skb_put(*skb
, 2) ) = chan
->callref
;
333 *(skb_put(*skb
, 1)) = chan
->layer2link
;
334 *(skb_put(*skb
, 1)) = chan
->r_refnum
;
339 int capi_disc_req(ushort callref
, struct sk_buff
**skb
, u_char cause
)
342 if ((*skb
= dev_alloc_skb(6)) == NULL
) {
344 printk(KERN_WARNING
"capi_disc_req: alloc_skb failed\n");
348 *((ushort
*) skb_put(*skb
, 2) ) = callref
;
350 *(skb_put(*skb
, 1)) = 2; /* Cause.Length = 2; */
351 *(skb_put(*skb
, 1)) = 0x80;
352 *(skb_put(*skb
, 1)) = 0x80 | cause
;
355 * Change it: we should send 'Sic transit gloria Mundi' here ;-)
358 *(skb_put(*skb
, 1)) = 0; /* UTUS.Length = 0; */
363 int capi_disc_resp(struct pcbit_chan
*chan
, struct sk_buff
**skb
)
365 if ((*skb
= dev_alloc_skb(2)) == NULL
) {
367 printk(KERN_WARNING
"capi_disc_resp: alloc_skb failed\n");
371 *((ushort
*) skb_put(*skb
, 2)) = chan
->callref
;
378 * Decoding of CAPI messages
382 int capi_decode_conn_ind(struct pcbit_chan
* chan
,
384 struct callb_data
*info
)
388 /* Call Reference [CAPI] */
389 chan
->callref
= *((ushort
*) skb
->data
);
393 printk(KERN_DEBUG
"Call Reference: %04x\n", chan
->callref
);
396 /* Channel Identification */
400 Octect 3 = 0100 10CC - [ 7 Basic, 4 , 2-1 chan ]
403 CIlen
= skb
->data
[0];
407 if ( ((skb
->data
[1]) & 0xFC) == 0x48 )
408 printk(KERN_DEBUG
"decode_conn_ind: chan ok\n");
409 printk(KERN_DEBUG
"phyChan = %d\n", skb
->data
[1] & 0x03);
412 printk(KERN_DEBUG
"conn_ind: CIlen = %d\n", CIlen
);
414 skb_pull(skb
, CIlen
+ 1);
416 /* Calling Party Number */
417 /* An "additional service" as far as Portugal Telecom is concerned */
425 printk(KERN_DEBUG
"CPN: Octect 3 %02x\n", skb
->data
[1]);
427 if ((skb
->data
[1] & 0x80) == 0)
430 if (!(info
->data
.setup
.CallingPN
= kmalloc(len
- count
+ 1, GFP_ATOMIC
)))
433 memcpy(info
->data
.setup
.CallingPN
, skb
->data
+ count
+ 1,
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 memcpy(info
->data
.setup
.CalledPN
, skb
->data
+ count
+ 1,
463 info
->data
.setup
.CalledPN
[len
- count
] = 0;
467 info
->data
.setup
.CalledPN
= NULL
;
468 printk(KERN_DEBUG
"NULL CalledPN\n");
471 skb_pull(skb
, len
+ 1);
473 /* Called Party Subaddress */
474 skb_pull(skb
, skb
->data
[0] + 1);
477 skb_pull(skb
, skb
->data
[0] + 1);
480 skb_pull(skb
, skb
->data
[0] + 1);
483 skb_pull(skb
, skb
->data
[0] + 1);
492 int capi_decode_conn_conf(struct pcbit_chan
* chan
, struct sk_buff
*skb
,
497 chan
->callref
= *((ushort
*) skb
->data
); /* Update CallReference */
500 errcode
= *((ushort
*) skb
->data
); /* read errcode */
503 *complete
= *(skb
->data
);
507 /* This is actually a firmware bug */
510 printk(KERN_DEBUG
"complete=%02x\n", *complete
);
515 /* Optional Bearer Capability */
516 skb_pull(skb
, *(skb
->data
) + 1);
518 /* Channel Identification */
519 skb_pull(skb
, *(skb
->data
) + 1);
521 /* High Layer Compatibility follows */
522 skb_pull(skb
, *(skb
->data
) + 1);
527 int capi_decode_conn_actv_ind(struct pcbit_chan
* chan
, struct sk_buff
*skb
)
534 /* Yet Another Bearer Capability */
535 skb_pull(skb
, *(skb
->data
) + 1);
538 /* Connected Party Number */
542 if (len
> 1 && len
< 31) {
543 memcpy(str
, skb
->data
+ 2, len
- 1);
545 printk(KERN_DEBUG
"Connected Party Number: %s\n", str
);
548 printk(KERN_DEBUG
"actv_ind CPN len = %d\n", len
);
551 skb_pull(skb
, len
+ 1);
553 /* Connected Subaddress */
554 skb_pull(skb
, *(skb
->data
) + 1);
556 /* Low Layer Capability */
557 skb_pull(skb
, *(skb
->data
) + 1);
559 /* High Layer Capability */
560 skb_pull(skb
, *(skb
->data
) + 1);
565 int capi_decode_conn_actv_conf(struct pcbit_chan
* chan
, struct sk_buff
*skb
)
569 errcode
= *((ushort
*) skb
->data
);
572 /* Channel Identification
573 skb_pull(skb, skb->data[0] + 1);
579 int capi_decode_sel_proto_conf(struct pcbit_chan
*chan
, struct sk_buff
*skb
)
583 chan
->layer2link
= *(skb
->data
);
586 errcode
= *((ushort
*) skb
->data
);
592 int capi_decode_actv_trans_conf(struct pcbit_chan
*chan
, struct sk_buff
*skb
)
596 if (chan
->layer2link
!= *(skb
->data
) )
597 printk("capi_decode_actv_trans_conf: layer2link doesn't match\n");
601 errcode
= *((ushort
*) skb
->data
);
607 int capi_decode_disc_ind(struct pcbit_chan
*chan
, struct sk_buff
*skb
)
620 for (i
=0; i
<len
; i
++)
621 printk(KERN_DEBUG
"Cause Octect %d: %02x\n", i
+3,
631 int capi_decode_debug_188(u_char
*hdr
, ushort hdrlen
)
638 if (len
< 64 && len
== hdrlen
- 1) {
639 memcpy(str
, hdr
+ 1, hdrlen
- 1);
644 printk("debug message incorrect\n");