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/kernel.h>
32 #include <linux/types.h>
33 #include <linux/slab.h>
36 #include <linux/skbuff.h>
39 #include <linux/string.h>
41 #include <linux/isdnif.h>
49 * Encoding of CAPI messages
53 int capi_conn_req(const char *calledPN, struct sk_buff **skb, int proto)
66 * CalledPN - 2 + strlen
73 len = 18 + strlen(calledPN);
75 if (proto == ISDN_PROTO_L2_TRANS)
78 if ((*skb = dev_alloc_skb(len)) == NULL) {
80 printk(KERN_WARNING "capi_conn_req: alloc_skb failed\n");
85 *((ushort *)skb_put(*skb, 2)) = AppInfoMask;
87 if (proto == ISDN_PROTO_L2_TRANS)
89 /* Bearer Capability - Mandatory*/
90 *(skb_put(*skb, 1)) = 3; /* BC0.Length */
91 *(skb_put(*skb, 1)) = 0x80; /* Speech */
92 *(skb_put(*skb, 1)) = 0x10; /* Circuit Mode */
93 *(skb_put(*skb, 1)) = 0x23; /* A-law */
95 /* Bearer Capability - Mandatory*/
96 *(skb_put(*skb, 1)) = 2; /* BC0.Length */
97 *(skb_put(*skb, 1)) = 0x88; /* Digital Information */
98 *(skb_put(*skb, 1)) = 0x90; /* BC0.Octect4 */
101 /* Bearer Capability - Optional*/
102 *(skb_put(*skb, 1)) = 0; /* BC1.Length = 0 */
104 *(skb_put(*skb, 1)) = 1; /* ChannelID.Length = 1 */
105 *(skb_put(*skb, 1)) = 0x83; /* Basic Interface - Any Channel */
107 *(skb_put(*skb, 1)) = 0; /* Keypad.Length = 0 */
110 *(skb_put(*skb, 1)) = 0; /* CallingPN.Length = 0 */
111 *(skb_put(*skb, 1)) = 0; /* CallingPSA.Length = 0 */
113 /* Called Party Number */
114 *(skb_put(*skb, 1)) = strlen(calledPN) + 1;
115 *(skb_put(*skb, 1)) = 0x81;
116 memcpy(skb_put(*skb, strlen(calledPN)), calledPN, strlen(calledPN));
120 *(skb_put(*skb, 1)) = 0; /* CalledPSA.Length = 0 */
122 /* LLC.Length = 0; */
123 /* HLC0.Length = 0; */
124 /* HLC1.Length = 0; */
125 /* UTUS.Length = 0; */
126 memset(skb_put(*skb, 4), 0, 4);
131 int capi_conn_resp(struct pcbit_chan *chan, struct sk_buff **skb)
134 if ((*skb = dev_alloc_skb(5)) == NULL) {
136 printk(KERN_WARNING "capi_conn_resp: alloc_skb failed\n");
140 *((ushort *)skb_put(*skb, 2)) = chan->callref;
141 *(skb_put(*skb, 1)) = 0x01; /* ACCEPT_CALL */
142 *(skb_put(*skb, 1)) = 0;
143 *(skb_put(*skb, 1)) = 0;
148 int capi_conn_active_req(struct pcbit_chan *chan, struct sk_buff **skb)
154 if ((*skb = dev_alloc_skb(8)) == NULL) {
156 printk(KERN_WARNING "capi_conn_active_req: alloc_skb failed\n");
160 *((ushort *)skb_put(*skb, 2)) = chan->callref;
163 printk(KERN_DEBUG "Call Reference: %04x\n", chan->callref);
166 *(skb_put(*skb, 1)) = 0; /* BC.Length = 0; */
167 *(skb_put(*skb, 1)) = 0; /* ConnectedPN.Length = 0 */
168 *(skb_put(*skb, 1)) = 0; /* PSA.Length */
169 *(skb_put(*skb, 1)) = 0; /* LLC.Length = 0; */
170 *(skb_put(*skb, 1)) = 0; /* HLC.Length = 0; */
171 *(skb_put(*skb, 1)) = 0; /* UTUS.Length = 0; */
176 int capi_conn_active_resp(struct pcbit_chan *chan, struct sk_buff **skb)
182 if ((*skb = dev_alloc_skb(2)) == NULL) {
184 printk(KERN_WARNING "capi_conn_active_resp: alloc_skb failed\n");
188 *((ushort *)skb_put(*skb, 2)) = chan->callref;
194 int capi_select_proto_req(struct pcbit_chan *chan, struct sk_buff **skb,
202 if ((*skb = dev_alloc_skb(18)) == NULL) {
204 printk(KERN_WARNING "capi_select_proto_req: alloc_skb failed\n");
208 *((ushort *)skb_put(*skb, 2)) = chan->callref;
210 /* Layer2 protocol */
212 switch (chan->proto) {
213 case ISDN_PROTO_L2_X75I:
214 *(skb_put(*skb, 1)) = 0x05; /* LAPB */
216 case ISDN_PROTO_L2_HDLC:
217 *(skb_put(*skb, 1)) = 0x02;
219 case ISDN_PROTO_L2_TRANS:
223 *(skb_put(*skb, 1)) = 0x06;
227 printk(KERN_DEBUG "Transparent\n");
229 *(skb_put(*skb, 1)) = 0x03;
233 *(skb_put(*skb, 1)) = (outgoing ? 0x02 : 0x42); /* Don't ask */
234 *(skb_put(*skb, 1)) = 0x00;
236 *((ushort *) skb_put(*skb, 2)) = MRU;
239 *(skb_put(*skb, 1)) = 0x08; /* Modulo */
240 *(skb_put(*skb, 1)) = 0x07; /* Max Window */
242 *(skb_put(*skb, 1)) = 0x01; /* No Layer3 Protocol */
245 * 2 - layer3 MTU [10]
248 * - layer1 proto [14]
251 * - layer1dataformat [17]
254 memset(skb_put(*skb, 8), 0, 8);
260 int capi_activate_transp_req(struct pcbit_chan *chan, struct sk_buff **skb)
263 if ((*skb = dev_alloc_skb(7)) == NULL) {
265 printk(KERN_WARNING "capi_activate_transp_req: alloc_skb failed\n");
269 *((ushort *)skb_put(*skb, 2)) = chan->callref;
272 *(skb_put(*skb, 1)) = chan->layer2link; /* Layer2 id */
273 *(skb_put(*skb, 1)) = 0x00; /* Transmit by default */
275 *((ushort *) skb_put(*skb, 2)) = MRU;
277 *(skb_put(*skb, 1)) = 0x01; /* Enables reception*/
282 int capi_tdata_req(struct pcbit_chan *chan, struct sk_buff *skb)
297 if (skb_headroom(skb) < 10)
299 printk(KERN_CRIT "No headspace (%u) on headroom %p for capi header\n", skb_headroom(skb), skb);
306 *((u16 *) (skb->data)) = chan->callref;
307 skb->data[2] = chan->layer2link;
308 *((u16 *) (skb->data + 3)) = data_len;
310 chan->s_refnum = (chan->s_refnum + 1) % 8;
311 *((u32 *) (skb->data + 5)) = chan->s_refnum;
313 skb->data[9] = 0; /* HDLC frame number */
318 int capi_tdata_resp(struct pcbit_chan *chan, struct sk_buff **skb)
321 if ((*skb = dev_alloc_skb(4)) == NULL) {
323 printk(KERN_WARNING "capi_tdata_resp: alloc_skb failed\n");
327 *((ushort *)skb_put(*skb, 2)) = chan->callref;
329 *(skb_put(*skb, 1)) = chan->layer2link;
330 *(skb_put(*skb, 1)) = chan->r_refnum;
335 int capi_disc_req(ushort callref, struct sk_buff **skb, u_char cause)
338 if ((*skb = dev_alloc_skb(6)) == NULL) {
340 printk(KERN_WARNING "capi_disc_req: alloc_skb failed\n");
344 *((ushort *)skb_put(*skb, 2)) = callref;
346 *(skb_put(*skb, 1)) = 2; /* Cause.Length = 2; */
347 *(skb_put(*skb, 1)) = 0x80;
348 *(skb_put(*skb, 1)) = 0x80 | cause;
351 * Change it: we should send 'Sic transit gloria Mundi' here ;-)
354 *(skb_put(*skb, 1)) = 0; /* UTUS.Length = 0; */
359 int capi_disc_resp(struct pcbit_chan *chan, struct sk_buff **skb)
361 if ((*skb = dev_alloc_skb(2)) == NULL) {
363 printk(KERN_WARNING "capi_disc_resp: alloc_skb failed\n");
367 *((ushort *)skb_put(*skb, 2)) = chan->callref;
374 * Decoding of CAPI messages
378 int capi_decode_conn_ind(struct pcbit_chan *chan,
380 struct callb_data *info)
384 /* Call Reference [CAPI] */
385 chan->callref = *((ushort *)skb->data);
389 printk(KERN_DEBUG "Call Reference: %04x\n", chan->callref);
392 /* Channel Identification */
396 Octect 3 = 0100 10CC - [ 7 Basic, 4 , 2-1 chan ]
399 CIlen = skb->data[0];
403 if (((skb->data[1]) & 0xFC) == 0x48)
404 printk(KERN_DEBUG "decode_conn_ind: chan ok\n");
405 printk(KERN_DEBUG "phyChan = %d\n", skb->data[1] & 0x03);
408 printk(KERN_DEBUG "conn_ind: CIlen = %d\n", CIlen);
410 skb_pull(skb, CIlen + 1);
412 /* Calling Party Number */
413 /* An "additional service" as far as Portugal Telecom is concerned */
421 printk(KERN_DEBUG "CPN: Octect 3 %02x\n", skb->data[1]);
423 if ((skb->data[1] & 0x80) == 0)
426 if (!(info->data.setup.CallingPN = kmalloc(len - count + 1, GFP_ATOMIC)))
429 skb_copy_from_linear_data_offset(skb, count + 1,
430 info->data.setup.CallingPN,
432 info->data.setup.CallingPN[len - count] = 0;
436 info->data.setup.CallingPN = NULL;
437 printk(KERN_DEBUG "NULL CallingPN\n");
440 skb_pull(skb, len + 1);
442 /* Calling Party Subaddress */
443 skb_pull(skb, skb->data[0] + 1);
445 /* Called Party Number */
452 if ((skb->data[1] & 0x80) == 0)
455 if (!(info->data.setup.CalledPN = kmalloc(len - count + 1, GFP_ATOMIC)))
458 skb_copy_from_linear_data_offset(skb, count + 1,
459 info->data.setup.CalledPN,
461 info->data.setup.CalledPN[len - count] = 0;
465 info->data.setup.CalledPN = NULL;
466 printk(KERN_DEBUG "NULL CalledPN\n");
469 skb_pull(skb, len + 1);
471 /* Called Party Subaddress */
472 skb_pull(skb, skb->data[0] + 1);
475 skb_pull(skb, skb->data[0] + 1);
478 skb_pull(skb, skb->data[0] + 1);
481 skb_pull(skb, skb->data[0] + 1);
490 int capi_decode_conn_conf(struct pcbit_chan *chan, struct sk_buff *skb,
495 chan->callref = *((ushort *)skb->data); /* Update CallReference */
498 errcode = *((ushort *) skb->data); /* read errcode */
501 *complete = *(skb->data);
505 /* This is actually a firmware bug */
508 printk(KERN_DEBUG "complete=%02x\n", *complete);
513 /* Optional Bearer Capability */
514 skb_pull(skb, *(skb->data) + 1);
516 /* Channel Identification */
517 skb_pull(skb, *(skb->data) + 1);
519 /* High Layer Compatibility follows */
520 skb_pull(skb, *(skb->data) + 1);
525 int capi_decode_conn_actv_ind(struct pcbit_chan *chan, struct sk_buff *skb)
532 /* Yet Another Bearer Capability */
533 skb_pull(skb, *(skb->data) + 1);
536 /* Connected Party Number */
540 if (len > 1 && len < 31) {
541 skb_copy_from_linear_data_offset(skb, 2, str, len - 1);
543 printk(KERN_DEBUG "Connected Party Number: %s\n", str);
546 printk(KERN_DEBUG "actv_ind CPN len = %d\n", len);
549 skb_pull(skb, len + 1);
551 /* Connected Subaddress */
552 skb_pull(skb, *(skb->data) + 1);
554 /* Low Layer Capability */
555 skb_pull(skb, *(skb->data) + 1);
557 /* High Layer Capability */
558 skb_pull(skb, *(skb->data) + 1);
563 int capi_decode_conn_actv_conf(struct pcbit_chan *chan, struct sk_buff *skb)
567 errcode = *((ushort *)skb->data);
570 /* Channel Identification
571 skb_pull(skb, skb->data[0] + 1);
577 int capi_decode_sel_proto_conf(struct pcbit_chan *chan, struct sk_buff *skb)
581 chan->layer2link = *(skb->data);
584 errcode = *((ushort *)skb->data);
590 int capi_decode_actv_trans_conf(struct pcbit_chan *chan, struct sk_buff *skb)
594 if (chan->layer2link != *(skb->data))
595 printk("capi_decode_actv_trans_conf: layer2link doesn't match\n");
599 errcode = *((ushort *)skb->data);
605 int capi_decode_disc_ind(struct pcbit_chan *chan, struct sk_buff *skb)
618 for (i = 0; i < len; i++)
619 printk(KERN_DEBUG "Cause Octect %d: %02x\n", i + 3,
629 int capi_decode_debug_188(u_char *hdr, ushort hdrlen)
636 if (len < 64 && len == hdrlen - 1) {
637 memcpy(str, hdr + 1, hdrlen - 1);
642 printk("debug message incorrect\n");