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 */
96 /* Bearer Capability - Mandatory*/
97 *(skb_put(*skb, 1)) = 2; /* BC0.Length */
98 *(skb_put(*skb, 1)) = 0x88; /* Digital Information */
99 *(skb_put(*skb, 1)) = 0x90; /* BC0.Octect4 */
102 /* Bearer Capability - Optional*/
103 *(skb_put(*skb, 1)) = 0; /* BC1.Length = 0 */
105 *(skb_put(*skb, 1)) = 1; /* ChannelID.Length = 1 */
106 *(skb_put(*skb, 1)) = 0x83; /* Basic Interface - Any Channel */
108 *(skb_put(*skb, 1)) = 0; /* Keypad.Length = 0 */
111 *(skb_put(*skb, 1)) = 0; /* CallingPN.Length = 0 */
112 *(skb_put(*skb, 1)) = 0; /* CallingPSA.Length = 0 */
114 /* Called Party Number */
115 *(skb_put(*skb, 1)) = strlen(calledPN) + 1;
116 *(skb_put(*skb, 1)) = 0x81;
117 memcpy(skb_put(*skb, strlen(calledPN)), calledPN, strlen(calledPN));
121 *(skb_put(*skb, 1)) = 0; /* CalledPSA.Length = 0 */
123 /* LLC.Length = 0; */
124 /* HLC0.Length = 0; */
125 /* HLC1.Length = 0; */
126 /* UTUS.Length = 0; */
127 memset(skb_put(*skb, 4), 0, 4);
132 int capi_conn_resp(struct pcbit_chan *chan, struct sk_buff **skb)
135 if ((*skb = dev_alloc_skb(5)) == NULL) {
137 printk(KERN_WARNING "capi_conn_resp: alloc_skb failed\n");
141 *((ushort *)skb_put(*skb, 2)) = chan->callref;
142 *(skb_put(*skb, 1)) = 0x01; /* ACCEPT_CALL */
143 *(skb_put(*skb, 1)) = 0;
144 *(skb_put(*skb, 1)) = 0;
149 int capi_conn_active_req(struct pcbit_chan *chan, struct sk_buff **skb)
155 if ((*skb = dev_alloc_skb(8)) == NULL) {
157 printk(KERN_WARNING "capi_conn_active_req: alloc_skb failed\n");
161 *((ushort *)skb_put(*skb, 2)) = chan->callref;
164 printk(KERN_DEBUG "Call Reference: %04x\n", chan->callref);
167 *(skb_put(*skb, 1)) = 0; /* BC.Length = 0; */
168 *(skb_put(*skb, 1)) = 0; /* ConnectedPN.Length = 0 */
169 *(skb_put(*skb, 1)) = 0; /* PSA.Length */
170 *(skb_put(*skb, 1)) = 0; /* LLC.Length = 0; */
171 *(skb_put(*skb, 1)) = 0; /* HLC.Length = 0; */
172 *(skb_put(*skb, 1)) = 0; /* UTUS.Length = 0; */
177 int capi_conn_active_resp(struct pcbit_chan *chan, struct sk_buff **skb)
183 if ((*skb = dev_alloc_skb(2)) == NULL) {
185 printk(KERN_WARNING "capi_conn_active_resp: alloc_skb failed\n");
189 *((ushort *)skb_put(*skb, 2)) = chan->callref;
195 int capi_select_proto_req(struct pcbit_chan *chan, struct sk_buff **skb,
203 if ((*skb = dev_alloc_skb(18)) == NULL) {
205 printk(KERN_WARNING "capi_select_proto_req: alloc_skb failed\n");
209 *((ushort *)skb_put(*skb, 2)) = chan->callref;
211 /* Layer2 protocol */
213 switch (chan->proto) {
214 case ISDN_PROTO_L2_X75I:
215 *(skb_put(*skb, 1)) = 0x05; /* LAPB */
217 case ISDN_PROTO_L2_HDLC:
218 *(skb_put(*skb, 1)) = 0x02;
220 case ISDN_PROTO_L2_TRANS:
224 *(skb_put(*skb, 1)) = 0x06;
228 printk(KERN_DEBUG "Transparent\n");
230 *(skb_put(*skb, 1)) = 0x03;
234 *(skb_put(*skb, 1)) = (outgoing ? 0x02 : 0x42); /* Don't ask */
235 *(skb_put(*skb, 1)) = 0x00;
237 *((ushort *) skb_put(*skb, 2)) = MRU;
240 *(skb_put(*skb, 1)) = 0x08; /* Modulo */
241 *(skb_put(*skb, 1)) = 0x07; /* Max Window */
243 *(skb_put(*skb, 1)) = 0x01; /* No Layer3 Protocol */
246 * 2 - layer3 MTU [10]
249 * - layer1 proto [14]
252 * - layer1dataformat [17]
255 memset(skb_put(*skb, 8), 0, 8);
261 int capi_activate_transp_req(struct pcbit_chan *chan, struct sk_buff **skb)
264 if ((*skb = dev_alloc_skb(7)) == NULL) {
266 printk(KERN_WARNING "capi_activate_transp_req: alloc_skb failed\n");
270 *((ushort *)skb_put(*skb, 2)) = chan->callref;
273 *(skb_put(*skb, 1)) = chan->layer2link; /* Layer2 id */
274 *(skb_put(*skb, 1)) = 0x00; /* Transmit by default */
276 *((ushort *) skb_put(*skb, 2)) = MRU;
278 *(skb_put(*skb, 1)) = 0x01; /* Enables reception*/
283 int capi_tdata_req(struct pcbit_chan *chan, struct sk_buff *skb)
298 if (skb_headroom(skb) < 10)
300 printk(KERN_CRIT "No headspace (%u) on headroom %p for capi header\n", skb_headroom(skb), skb);
307 *((u16 *) (skb->data)) = chan->callref;
308 skb->data[2] = chan->layer2link;
309 *((u16 *) (skb->data + 3)) = data_len;
311 chan->s_refnum = (chan->s_refnum + 1) % 8;
312 *((u32 *) (skb->data + 5)) = chan->s_refnum;
314 skb->data[9] = 0; /* HDLC frame number */
319 int capi_tdata_resp(struct pcbit_chan *chan, struct sk_buff **skb)
322 if ((*skb = dev_alloc_skb(4)) == NULL) {
324 printk(KERN_WARNING "capi_tdata_resp: alloc_skb failed\n");
328 *((ushort *)skb_put(*skb, 2)) = chan->callref;
330 *(skb_put(*skb, 1)) = chan->layer2link;
331 *(skb_put(*skb, 1)) = chan->r_refnum;
336 int capi_disc_req(ushort callref, struct sk_buff **skb, u_char cause)
339 if ((*skb = dev_alloc_skb(6)) == NULL) {
341 printk(KERN_WARNING "capi_disc_req: alloc_skb failed\n");
345 *((ushort *)skb_put(*skb, 2)) = callref;
347 *(skb_put(*skb, 1)) = 2; /* Cause.Length = 2; */
348 *(skb_put(*skb, 1)) = 0x80;
349 *(skb_put(*skb, 1)) = 0x80 | cause;
352 * Change it: we should send 'Sic transit gloria Mundi' here ;-)
355 *(skb_put(*skb, 1)) = 0; /* UTUS.Length = 0; */
360 int capi_disc_resp(struct pcbit_chan *chan, struct sk_buff **skb)
362 if ((*skb = dev_alloc_skb(2)) == NULL) {
364 printk(KERN_WARNING "capi_disc_resp: alloc_skb failed\n");
368 *((ushort *)skb_put(*skb, 2)) = chan->callref;
375 * Decoding of CAPI messages
379 int capi_decode_conn_ind(struct pcbit_chan *chan,
381 struct callb_data *info)
385 /* Call Reference [CAPI] */
386 chan->callref = *((ushort *)skb->data);
390 printk(KERN_DEBUG "Call Reference: %04x\n", chan->callref);
393 /* Channel Identification */
397 Octect 3 = 0100 10CC - [ 7 Basic, 4 , 2-1 chan ]
400 CIlen = skb->data[0];
404 if (((skb->data[1]) & 0xFC) == 0x48)
405 printk(KERN_DEBUG "decode_conn_ind: chan ok\n");
406 printk(KERN_DEBUG "phyChan = %d\n", skb->data[1] & 0x03);
409 printk(KERN_DEBUG "conn_ind: CIlen = %d\n", CIlen);
411 skb_pull(skb, CIlen + 1);
413 /* Calling Party Number */
414 /* An "additional service" as far as Portugal Telecom is concerned */
422 printk(KERN_DEBUG "CPN: Octect 3 %02x\n", skb->data[1]);
424 if ((skb->data[1] & 0x80) == 0)
427 if (!(info->data.setup.CallingPN = kmalloc(len - count + 1, GFP_ATOMIC)))
430 skb_copy_from_linear_data_offset(skb, count + 1,
431 info->data.setup.CallingPN,
433 info->data.setup.CallingPN[len - count] = 0;
437 info->data.setup.CallingPN = NULL;
438 printk(KERN_DEBUG "NULL CallingPN\n");
441 skb_pull(skb, len + 1);
443 /* Calling Party Subaddress */
444 skb_pull(skb, skb->data[0] + 1);
446 /* Called Party Number */
453 if ((skb->data[1] & 0x80) == 0)
456 if (!(info->data.setup.CalledPN = kmalloc(len - count + 1, GFP_ATOMIC)))
459 skb_copy_from_linear_data_offset(skb, count + 1,
460 info->data.setup.CalledPN,
462 info->data.setup.CalledPN[len - count] = 0;
466 info->data.setup.CalledPN = NULL;
467 printk(KERN_DEBUG "NULL CalledPN\n");
470 skb_pull(skb, len + 1);
472 /* Called Party Subaddress */
473 skb_pull(skb, skb->data[0] + 1);
476 skb_pull(skb, skb->data[0] + 1);
479 skb_pull(skb, skb->data[0] + 1);
482 skb_pull(skb, skb->data[0] + 1);
491 int capi_decode_conn_conf(struct pcbit_chan *chan, struct sk_buff *skb,
496 chan->callref = *((ushort *)skb->data); /* Update CallReference */
499 errcode = *((ushort *) skb->data); /* read errcode */
502 *complete = *(skb->data);
506 /* This is actually a firmware bug */
509 printk(KERN_DEBUG "complete=%02x\n", *complete);
514 /* Optional Bearer Capability */
515 skb_pull(skb, *(skb->data) + 1);
517 /* Channel Identification */
518 skb_pull(skb, *(skb->data) + 1);
520 /* High Layer Compatibility follows */
521 skb_pull(skb, *(skb->data) + 1);
526 int capi_decode_conn_actv_ind(struct pcbit_chan *chan, struct sk_buff *skb)
533 /* Yet Another Bearer Capability */
534 skb_pull(skb, *(skb->data) + 1);
537 /* Connected Party Number */
541 if (len > 1 && len < 31) {
542 skb_copy_from_linear_data_offset(skb, 2, str, len - 1);
544 printk(KERN_DEBUG "Connected Party Number: %s\n", str);
547 printk(KERN_DEBUG "actv_ind CPN len = %d\n", len);
550 skb_pull(skb, len + 1);
552 /* Connected Subaddress */
553 skb_pull(skb, *(skb->data) + 1);
555 /* Low Layer Capability */
556 skb_pull(skb, *(skb->data) + 1);
558 /* High Layer Capability */
559 skb_pull(skb, *(skb->data) + 1);
564 int capi_decode_conn_actv_conf(struct pcbit_chan *chan, struct sk_buff *skb)
568 errcode = *((ushort *)skb->data);
571 /* Channel Identification
572 skb_pull(skb, skb->data[0] + 1);
578 int capi_decode_sel_proto_conf(struct pcbit_chan *chan, struct sk_buff *skb)
582 chan->layer2link = *(skb->data);
585 errcode = *((ushort *)skb->data);
591 int capi_decode_actv_trans_conf(struct pcbit_chan *chan, struct sk_buff *skb)
595 if (chan->layer2link != *(skb->data))
596 printk("capi_decode_actv_trans_conf: layer2link doesn't match\n");
600 errcode = *((ushort *)skb->data);
606 int capi_decode_disc_ind(struct pcbit_chan *chan, struct sk_buff *skb)
619 for (i = 0; i < len; i++)
620 printk(KERN_DEBUG "Cause Octect %d: %02x\n", i + 3,
630 int capi_decode_debug_188(u_char *hdr, ushort hdrlen)
637 if (len < 64 && len == hdrlen - 1) {
638 memcpy(str, hdr + 1, hdrlen - 1);
643 printk("debug message incorrect\n");