blob: 4e3cbf857d6066cfceff3ff3422c9b5cfdbd3318 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * CAPI encoder/decoder for
3 * Portugal Telecom CAPI 2.0
4 *
5 * Copyright (C) 1996 Universidade de Lisboa
Joe Perches475be4d2012-02-19 19:52:38 -08006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Written by Pedro Roque Marques (roque@di.fc.ul.pt)
8 *
Joe Perches475be4d2012-02-19 19:52:38 -08009 * This software may be used and distributed according to the terms of
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * the GNU General Public License, incorporated herein by reference.
11 *
12 * Not compatible with the AVM Gmbh. CAPI 2.0
13 *
14 */
15
16/*
17 * Documentation:
Jan Engelhardt96de0e22007-10-19 23:21:04 +020018 * - "Common ISDN API - Perfil Português - Versão 2.1",
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Telecom Portugal, Fev 1992.
Jan Engelhardt96de0e22007-10-19 23:21:04 +020020 * - "Common ISDN API - Especificação de protocolos para
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * acesso aos canais B", Inesc, Jan 1994.
22 */
23
24/*
25 * TODO: better decoding of Information Elements
26 * for debug purposes mainly
27 * encode our number in CallerPN and ConnectedPN
28 */
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/string.h>
31#include <linux/kernel.h>
32
33#include <linux/types.h>
34#include <linux/slab.h>
35#include <linux/mm.h>
36
37#include <linux/skbuff.h>
38
39#include <asm/io.h>
40#include <asm/string.h>
41
42#include <linux/isdnif.h>
43
44#include "pcbit.h"
45#include "edss1.h"
46#include "capi.h"
47
48
49/*
50 * Encoding of CAPI messages
51 *
52 */
53
Joe Perches475be4d2012-02-19 19:52:38 -080054int capi_conn_req(const char *calledPN, struct sk_buff **skb, int proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Joe Perches475be4d2012-02-19 19:52:38 -080056 ushort len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Joe Perches475be4d2012-02-19 19:52:38 -080058 /*
59 * length
60 * AppInfoMask - 2
61 * BC0 - 3
62 * BC1 - 1
63 * Chan - 2
64 * Keypad - 1
65 * CPN - 1
66 * CPSA - 1
67 * CalledPN - 2 + strlen
68 * CalledPSA - 1
69 * rest... - 4
70 * ----------------
71 * Total 18 + strlen
72 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Joe Perches475be4d2012-02-19 19:52:38 -080074 len = 18 + strlen(calledPN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 if (proto == ISDN_PROTO_L2_TRANS)
77 len++;
78
79 if ((*skb = dev_alloc_skb(len)) == NULL) {
Joe Perches475be4d2012-02-19 19:52:38 -080080
81 printk(KERN_WARNING "capi_conn_req: alloc_skb failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return -1;
83 }
84
Joe Perches475be4d2012-02-19 19:52:38 -080085 /* InfoElmMask */
86 *((ushort *)skb_put(*skb, 2)) = AppInfoMask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 if (proto == ISDN_PROTO_L2_TRANS)
89 {
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 */
95 }
96 else
97 {
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 */
102 }
103
Joe Perches475be4d2012-02-19 19:52:38 -0800104 /* Bearer Capability - Optional*/
105 *(skb_put(*skb, 1)) = 0; /* BC1.Length = 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Joe Perches475be4d2012-02-19 19:52:38 -0800107 *(skb_put(*skb, 1)) = 1; /* ChannelID.Length = 1 */
108 *(skb_put(*skb, 1)) = 0x83; /* Basic Interface - Any Channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Joe Perches475be4d2012-02-19 19:52:38 -0800110 *(skb_put(*skb, 1)) = 0; /* Keypad.Length = 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Joe Perches475be4d2012-02-19 19:52:38 -0800113 *(skb_put(*skb, 1)) = 0; /* CallingPN.Length = 0 */
114 *(skb_put(*skb, 1)) = 0; /* CallingPSA.Length = 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Joe Perches475be4d2012-02-19 19:52:38 -0800116 /* 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Joe Perches475be4d2012-02-19 19:52:38 -0800121 /* '#' */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Joe Perches475be4d2012-02-19 19:52:38 -0800123 *(skb_put(*skb, 1)) = 0; /* CalledPSA.Length = 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Joe Perches475be4d2012-02-19 19:52:38 -0800125 /* LLC.Length = 0; */
126 /* HLC0.Length = 0; */
127 /* HLC1.Length = 0; */
128 /* UTUS.Length = 0; */
129 memset(skb_put(*skb, 4), 0, 4);
130
131 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Joe Perches475be4d2012-02-19 19:52:38 -0800134int capi_conn_resp(struct pcbit_chan *chan, struct sk_buff **skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Joe Perches475be4d2012-02-19 19:52:38 -0800136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if ((*skb = dev_alloc_skb(5)) == NULL) {
Joe Perches475be4d2012-02-19 19:52:38 -0800138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 printk(KERN_WARNING "capi_conn_resp: alloc_skb failed\n");
140 return -1;
141 }
142
Joe Perches475be4d2012-02-19 19:52:38 -0800143 *((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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Joe Perches475be4d2012-02-19 19:52:38 -0800148 return 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Joe Perches475be4d2012-02-19 19:52:38 -0800151int capi_conn_active_req(struct pcbit_chan *chan, struct sk_buff **skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Joe Perches475be4d2012-02-19 19:52:38 -0800153 /*
154 * 8 bytes
155 */
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if ((*skb = dev_alloc_skb(8)) == NULL) {
Joe Perches475be4d2012-02-19 19:52:38 -0800158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 printk(KERN_WARNING "capi_conn_active_req: alloc_skb failed\n");
160 return -1;
161 }
162
Joe Perches475be4d2012-02-19 19:52:38 -0800163 *((ushort *)skb_put(*skb, 2)) = chan->callref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165#ifdef DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800166 printk(KERN_DEBUG "Call Reference: %04x\n", chan->callref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#endif
168
Joe Perches475be4d2012-02-19 19:52:38 -0800169 *(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; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 return 8;
177}
178
Joe Perches475be4d2012-02-19 19:52:38 -0800179int capi_conn_active_resp(struct pcbit_chan *chan, struct sk_buff **skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Joe Perches475be4d2012-02-19 19:52:38 -0800181 /*
182 * 2 bytes
183 */
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if ((*skb = dev_alloc_skb(2)) == NULL) {
Joe Perches475be4d2012-02-19 19:52:38 -0800186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 printk(KERN_WARNING "capi_conn_active_resp: alloc_skb failed\n");
188 return -1;
189 }
190
Joe Perches475be4d2012-02-19 19:52:38 -0800191 *((ushort *)skb_put(*skb, 2)) = chan->callref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Joe Perches475be4d2012-02-19 19:52:38 -0800193 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196
Joe Perches475be4d2012-02-19 19:52:38 -0800197int capi_select_proto_req(struct pcbit_chan *chan, struct sk_buff **skb,
198 int outgoing)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200
Joe Perches475be4d2012-02-19 19:52:38 -0800201 /*
202 * 18 bytes
203 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 if ((*skb = dev_alloc_skb(18)) == NULL) {
Joe Perches475be4d2012-02-19 19:52:38 -0800206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 printk(KERN_WARNING "capi_select_proto_req: alloc_skb failed\n");
208 return -1;
209 }
210
Joe Perches475be4d2012-02-19 19:52:38 -0800211 *((ushort *)skb_put(*skb, 2)) = chan->callref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Joe Perches475be4d2012-02-19 19:52:38 -0800213 /* Layer2 protocol */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Joe Perches475be4d2012-02-19 19:52:38 -0800215 switch (chan->proto) {
216 case ISDN_PROTO_L2_X75I:
217 *(skb_put(*skb, 1)) = 0x05; /* LAPB */
218 break;
219 case ISDN_PROTO_L2_HDLC:
220 *(skb_put(*skb, 1)) = 0x02;
221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 case ISDN_PROTO_L2_TRANS:
Joe Perches475be4d2012-02-19 19:52:38 -0800223 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 * Voice (a-law)
225 */
226 *(skb_put(*skb, 1)) = 0x06;
227 break;
Joe Perches475be4d2012-02-19 19:52:38 -0800228 default:
229#ifdef DEBUG
230 printk(KERN_DEBUG "Transparent\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231#endif
Joe Perches475be4d2012-02-19 19:52:38 -0800232 *(skb_put(*skb, 1)) = 0x03;
233 break;
234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Joe Perches475be4d2012-02-19 19:52:38 -0800236 *(skb_put(*skb, 1)) = (outgoing ? 0x02 : 0x42); /* Don't ask */
237 *(skb_put(*skb, 1)) = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Joe Perches475be4d2012-02-19 19:52:38 -0800239 *((ushort *) skb_put(*skb, 2)) = MRU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Joe Perches475be4d2012-02-19 19:52:38 -0800242 *(skb_put(*skb, 1)) = 0x08; /* Modulo */
243 *(skb_put(*skb, 1)) = 0x07; /* Max Window */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Joe Perches475be4d2012-02-19 19:52:38 -0800245 *(skb_put(*skb, 1)) = 0x01; /* No Layer3 Protocol */
246
247 /*
248 * 2 - layer3 MTU [10]
249 * - Modulo [12]
250 * - Window
251 * - layer1 proto [14]
252 * - bitrate
253 * - sub-channel [16]
254 * - layer1dataformat [17]
255 */
256
257 memset(skb_put(*skb, 8), 0, 8);
258
259 return 18;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262
263int capi_activate_transp_req(struct pcbit_chan *chan, struct sk_buff **skb)
264{
265
266 if ((*skb = dev_alloc_skb(7)) == NULL) {
Joe Perches475be4d2012-02-19 19:52:38 -0800267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 printk(KERN_WARNING "capi_activate_transp_req: alloc_skb failed\n");
269 return -1;
270 }
271
Joe Perches475be4d2012-02-19 19:52:38 -0800272 *((ushort *)skb_put(*skb, 2)) = chan->callref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Joe Perches475be4d2012-02-19 19:52:38 -0800275 *(skb_put(*skb, 1)) = chan->layer2link; /* Layer2 id */
276 *(skb_put(*skb, 1)) = 0x00; /* Transmit by default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Joe Perches475be4d2012-02-19 19:52:38 -0800278 *((ushort *) skb_put(*skb, 2)) = MRU;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Joe Perches475be4d2012-02-19 19:52:38 -0800280 *(skb_put(*skb, 1)) = 0x01; /* Enables reception*/
281
282 return 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Joe Perches475be4d2012-02-19 19:52:38 -0800285int capi_tdata_req(struct pcbit_chan *chan, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 ushort data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Joe Perches475be4d2012-02-19 19:52:38 -0800289
290 /*
291 * callref - 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 * layer2link - 1
Joe Perches475be4d2012-02-19 19:52:38 -0800293 * wBlockLength - 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * data - 4
295 * sernum - 1
296 */
Joe Perches475be4d2012-02-19 19:52:38 -0800297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 data_len = skb->len;
299
Joe Perches475be4d2012-02-19 19:52:38 -0800300 if (skb_headroom(skb) < 10)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 {
302 printk(KERN_CRIT "No headspace (%u) on headroom %p for capi header\n", skb_headroom(skb), skb);
303 }
304 else
Joe Perches475be4d2012-02-19 19:52:38 -0800305 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 skb_push(skb, 10);
307 }
308
309 *((u16 *) (skb->data)) = chan->callref;
310 skb->data[2] = chan->layer2link;
311 *((u16 *) (skb->data + 3)) = data_len;
312
313 chan->s_refnum = (chan->s_refnum + 1) % 8;
314 *((u32 *) (skb->data + 5)) = chan->s_refnum;
315
316 skb->data[9] = 0; /* HDLC frame number */
317
318 return 10;
319}
320
Joe Perches475be4d2012-02-19 19:52:38 -0800321int capi_tdata_resp(struct pcbit_chan *chan, struct sk_buff **skb)
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 if ((*skb = dev_alloc_skb(4)) == NULL) {
Joe Perches475be4d2012-02-19 19:52:38 -0800325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 printk(KERN_WARNING "capi_tdata_resp: alloc_skb failed\n");
327 return -1;
328 }
329
Joe Perches475be4d2012-02-19 19:52:38 -0800330 *((ushort *)skb_put(*skb, 2)) = chan->callref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Joe Perches475be4d2012-02-19 19:52:38 -0800332 *(skb_put(*skb, 1)) = chan->layer2link;
333 *(skb_put(*skb, 1)) = chan->r_refnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Joe Perches475be4d2012-02-19 19:52:38 -0800335 return (*skb)->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338int capi_disc_req(ushort callref, struct sk_buff **skb, u_char cause)
339{
340
341 if ((*skb = dev_alloc_skb(6)) == NULL) {
Joe Perches475be4d2012-02-19 19:52:38 -0800342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 printk(KERN_WARNING "capi_disc_req: alloc_skb failed\n");
344 return -1;
345 }
346
Joe Perches475be4d2012-02-19 19:52:38 -0800347 *((ushort *)skb_put(*skb, 2)) = callref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Joe Perches475be4d2012-02-19 19:52:38 -0800349 *(skb_put(*skb, 1)) = 2; /* Cause.Length = 2; */
350 *(skb_put(*skb, 1)) = 0x80;
351 *(skb_put(*skb, 1)) = 0x80 | cause;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Joe Perches475be4d2012-02-19 19:52:38 -0800353 /*
354 * Change it: we should send 'Sic transit gloria Mundi' here ;-)
355 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Joe Perches475be4d2012-02-19 19:52:38 -0800357 *(skb_put(*skb, 1)) = 0; /* UTUS.Length = 0; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Joe Perches475be4d2012-02-19 19:52:38 -0800359 return 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
362int capi_disc_resp(struct pcbit_chan *chan, struct sk_buff **skb)
363{
364 if ((*skb = dev_alloc_skb(2)) == NULL) {
Joe Perches475be4d2012-02-19 19:52:38 -0800365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 printk(KERN_WARNING "capi_disc_resp: alloc_skb failed\n");
367 return -1;
368 }
369
Joe Perches475be4d2012-02-19 19:52:38 -0800370 *((ushort *)skb_put(*skb, 2)) = chan->callref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Joe Perches475be4d2012-02-19 19:52:38 -0800372 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375
376/*
377 * Decoding of CAPI messages
378 *
379 */
380
Joe Perches475be4d2012-02-19 19:52:38 -0800381int capi_decode_conn_ind(struct pcbit_chan *chan,
382 struct sk_buff *skb,
383 struct callb_data *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Joe Perches475be4d2012-02-19 19:52:38 -0800385 int CIlen, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Joe Perches475be4d2012-02-19 19:52:38 -0800387 /* Call Reference [CAPI] */
388 chan->callref = *((ushort *)skb->data);
389 skb_pull(skb, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391#ifdef DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800392 printk(KERN_DEBUG "Call Reference: %04x\n", chan->callref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393#endif
394
Joe Perches475be4d2012-02-19 19:52:38 -0800395 /* Channel Identification */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Joe Perches475be4d2012-02-19 19:52:38 -0800397 /* Expect
398 Len = 1
399 Octect 3 = 0100 10CC - [ 7 Basic, 4 , 2-1 chan ]
400 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Joe Perches475be4d2012-02-19 19:52:38 -0800402 CIlen = skb->data[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403#ifdef DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800404 if (CIlen == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Joe Perches475be4d2012-02-19 19:52:38 -0800406 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);
409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 else
411 printk(KERN_DEBUG "conn_ind: CIlen = %d\n", CIlen);
412#endif
Joe Perches475be4d2012-02-19 19:52:38 -0800413 skb_pull(skb, CIlen + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Joe Perches475be4d2012-02-19 19:52:38 -0800415 /* Calling Party Number */
416 /* An "additional service" as far as Portugal Telecom is concerned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Joe Perches475be4d2012-02-19 19:52:38 -0800418 len = skb->data[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 if (len > 0) {
421 int count = 1;
Joe Perches475be4d2012-02-19 19:52:38 -0800422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423#ifdef DEBUG
424 printk(KERN_DEBUG "CPN: Octect 3 %02x\n", skb->data[1]);
425#endif
426 if ((skb->data[1] & 0x80) == 0)
427 count = 2;
Joe Perches475be4d2012-02-19 19:52:38 -0800428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (!(info->data.setup.CallingPN = kmalloc(len - count + 1, GFP_ATOMIC)))
430 return -1;
Joe Perches475be4d2012-02-19 19:52:38 -0800431
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300432 skb_copy_from_linear_data_offset(skb, count + 1,
433 info->data.setup.CallingPN,
434 len - count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 info->data.setup.CallingPN[len - count] = 0;
436
437 }
438 else {
439 info->data.setup.CallingPN = NULL;
440 printk(KERN_DEBUG "NULL CallingPN\n");
441 }
442
443 skb_pull(skb, len + 1);
444
Joe Perches475be4d2012-02-19 19:52:38 -0800445 /* Calling Party Subaddress */
446 skb_pull(skb, skb->data[0] + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Joe Perches475be4d2012-02-19 19:52:38 -0800448 /* Called Party Number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Joe Perches475be4d2012-02-19 19:52:38 -0800450 len = skb->data[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 if (len > 0) {
453 int count = 1;
Joe Perches475be4d2012-02-19 19:52:38 -0800454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if ((skb->data[1] & 0x80) == 0)
456 count = 2;
Joe Perches475be4d2012-02-19 19:52:38 -0800457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (!(info->data.setup.CalledPN = kmalloc(len - count + 1, GFP_ATOMIC)))
459 return -1;
Joe Perches475be4d2012-02-19 19:52:38 -0800460
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300461 skb_copy_from_linear_data_offset(skb, count + 1,
462 info->data.setup.CalledPN,
463 len - count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 info->data.setup.CalledPN[len - count] = 0;
465
466 }
467 else {
468 info->data.setup.CalledPN = NULL;
469 printk(KERN_DEBUG "NULL CalledPN\n");
470 }
471
472 skb_pull(skb, len + 1);
473
Joe Perches475be4d2012-02-19 19:52:38 -0800474 /* Called Party Subaddress */
475 skb_pull(skb, skb->data[0] + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Joe Perches475be4d2012-02-19 19:52:38 -0800477 /* LLC */
478 skb_pull(skb, skb->data[0] + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Joe Perches475be4d2012-02-19 19:52:38 -0800480 /* HLC */
481 skb_pull(skb, skb->data[0] + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Joe Perches475be4d2012-02-19 19:52:38 -0800483 /* U2U */
484 skb_pull(skb, skb->data[0] + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Joe Perches475be4d2012-02-19 19:52:38 -0800486 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
489/*
490 * returns errcode
491 */
492
Joe Perches475be4d2012-02-19 19:52:38 -0800493int capi_decode_conn_conf(struct pcbit_chan *chan, struct sk_buff *skb,
494 int *complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Joe Perches475be4d2012-02-19 19:52:38 -0800496 int errcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Joe Perches475be4d2012-02-19 19:52:38 -0800498 chan->callref = *((ushort *)skb->data); /* Update CallReference */
499 skb_pull(skb, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Joe Perches475be4d2012-02-19 19:52:38 -0800501 errcode = *((ushort *) skb->data); /* read errcode */
502 skb_pull(skb, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Joe Perches475be4d2012-02-19 19:52:38 -0800504 *complete = *(skb->data);
505 skb_pull(skb, 1);
506
507 /* FIX ME */
508 /* This is actually a firmware bug */
509 if (!*complete)
510 {
511 printk(KERN_DEBUG "complete=%02x\n", *complete);
512 *complete = 1;
513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515
Joe Perches475be4d2012-02-19 19:52:38 -0800516 /* Optional Bearer Capability */
517 skb_pull(skb, *(skb->data) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Joe Perches475be4d2012-02-19 19:52:38 -0800519 /* Channel Identification */
520 skb_pull(skb, *(skb->data) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Joe Perches475be4d2012-02-19 19:52:38 -0800522 /* High Layer Compatibility follows */
523 skb_pull(skb, *(skb->data) + 1);
524
525 return errcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
Joe Perches475be4d2012-02-19 19:52:38 -0800528int capi_decode_conn_actv_ind(struct pcbit_chan *chan, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Joe Perches475be4d2012-02-19 19:52:38 -0800530 ushort len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531#ifdef DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800532 char str[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533#endif
534
Joe Perches475be4d2012-02-19 19:52:38 -0800535 /* Yet Another Bearer Capability */
536 skb_pull(skb, *(skb->data) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Joe Perches475be4d2012-02-19 19:52:38 -0800538
539 /* Connected Party Number */
540 len = *(skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542#ifdef DEBUG
543 if (len > 1 && len < 31) {
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300544 skb_copy_from_linear_data_offset(skb, 2, str, len - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 str[len] = 0;
546 printk(KERN_DEBUG "Connected Party Number: %s\n", str);
547 }
548 else
549 printk(KERN_DEBUG "actv_ind CPN len = %d\n", len);
550#endif
551
Joe Perches475be4d2012-02-19 19:52:38 -0800552 skb_pull(skb, len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Joe Perches475be4d2012-02-19 19:52:38 -0800554 /* Connected Subaddress */
555 skb_pull(skb, *(skb->data) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Joe Perches475be4d2012-02-19 19:52:38 -0800557 /* Low Layer Capability */
558 skb_pull(skb, *(skb->data) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Joe Perches475be4d2012-02-19 19:52:38 -0800560 /* High Layer Capability */
561 skb_pull(skb, *(skb->data) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Joe Perches475be4d2012-02-19 19:52:38 -0800563 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Joe Perches475be4d2012-02-19 19:52:38 -0800566int capi_decode_conn_actv_conf(struct pcbit_chan *chan, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Joe Perches475be4d2012-02-19 19:52:38 -0800568 ushort errcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Joe Perches475be4d2012-02-19 19:52:38 -0800570 errcode = *((ushort *)skb->data);
571 skb_pull(skb, 2);
572
573 /* Channel Identification
574 skb_pull(skb, skb->data[0] + 1);
575 */
576 return errcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579
580int capi_decode_sel_proto_conf(struct pcbit_chan *chan, struct sk_buff *skb)
581{
Joe Perches475be4d2012-02-19 19:52:38 -0800582 ushort errcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Joe Perches475be4d2012-02-19 19:52:38 -0800584 chan->layer2link = *(skb->data);
585 skb_pull(skb, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Joe Perches475be4d2012-02-19 19:52:38 -0800587 errcode = *((ushort *)skb->data);
588 skb_pull(skb, 2);
589
590 return errcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593int capi_decode_actv_trans_conf(struct pcbit_chan *chan, struct sk_buff *skb)
594{
Joe Perches475be4d2012-02-19 19:52:38 -0800595 ushort errcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Joe Perches475be4d2012-02-19 19:52:38 -0800597 if (chan->layer2link != *(skb->data))
598 printk("capi_decode_actv_trans_conf: layer2link doesn't match\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Joe Perches475be4d2012-02-19 19:52:38 -0800600 skb_pull(skb, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Joe Perches475be4d2012-02-19 19:52:38 -0800602 errcode = *((ushort *)skb->data);
603 skb_pull(skb, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Joe Perches475be4d2012-02-19 19:52:38 -0800605 return errcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
608int capi_decode_disc_ind(struct pcbit_chan *chan, struct sk_buff *skb)
609{
Joe Perches475be4d2012-02-19 19:52:38 -0800610 ushort len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611#ifdef DEBUG
Joe Perches475be4d2012-02-19 19:52:38 -0800612 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613#endif
Joe Perches475be4d2012-02-19 19:52:38 -0800614 /* Cause */
615
616 len = *(skb->data);
617 skb_pull(skb, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619#ifdef DEBUG
620
Joe Perches475be4d2012-02-19 19:52:38 -0800621 for (i = 0; i < len; i++)
622 printk(KERN_DEBUG "Cause Octect %d: %02x\n", i + 3,
623 *(skb->data + i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624#endif
625
Joe Perches475be4d2012-02-19 19:52:38 -0800626 skb_pull(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Joe Perches475be4d2012-02-19 19:52:38 -0800628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631#ifdef DEBUG
632int capi_decode_debug_188(u_char *hdr, ushort hdrlen)
633{
Joe Perches475be4d2012-02-19 19:52:38 -0800634 char str[64];
635 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Joe Perches475be4d2012-02-19 19:52:38 -0800637 len = hdr[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Joe Perches475be4d2012-02-19 19:52:38 -0800639 if (len < 64 && len == hdrlen - 1) {
640 memcpy(str, hdr + 1, hdrlen - 1);
641 str[hdrlen - 1] = 0;
642 printk("%s\n", str);
643 }
644 else
645 printk("debug message incorrect\n");
646
647 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649#endif