blob: b501d76209f0240f11f5b04e28aa9512ec767f69 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: capiutil.c,v 1.13.6.4 2001/09/23 22:24:33 kai Exp $
2 *
3 * CAPI 2.0 convert capi message to capi message struct
4 *
5 * From CAPI 2.0 Development Kit AVM 1995 (msg.c)
6 * Rewritten for Linux 1996 by Carsten Paeth <calle@calle.de>
7 *
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
10 *
11 */
12
13#include <linux/module.h>
14#include <linux/string.h>
15#include <linux/ctype.h>
16#include <linux/stddef.h>
17#include <linux/kernel.h>
18#include <linux/mm.h>
19#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/isdn/capiutil.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23/* from CAPI2.0 DDK AVM Berlin GmbH */
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025typedef struct {
26 int typ;
27 size_t off;
28} _cdef;
29
30#define _CBYTE 1
31#define _CWORD 2
32#define _CDWORD 3
33#define _CSTRUCT 4
34#define _CMSTRUCT 5
35#define _CEND 6
36
37static _cdef cdef[] =
38{
Joe Perches475be4d2012-02-19 19:52:38 -080039 /*00 */
40 {_CEND},
41 /*01 */
42 {_CEND},
43 /*02 */
44 {_CEND},
45 /*03 */
46 {_CDWORD, offsetof(_cmsg, adr.adrController)},
47 /*04 */
48 {_CMSTRUCT, offsetof(_cmsg, AdditionalInfo)},
49 /*05 */
50 {_CSTRUCT, offsetof(_cmsg, B1configuration)},
51 /*06 */
52 {_CWORD, offsetof(_cmsg, B1protocol)},
53 /*07 */
54 {_CSTRUCT, offsetof(_cmsg, B2configuration)},
55 /*08 */
56 {_CWORD, offsetof(_cmsg, B2protocol)},
57 /*09 */
58 {_CSTRUCT, offsetof(_cmsg, B3configuration)},
59 /*0a */
60 {_CWORD, offsetof(_cmsg, B3protocol)},
61 /*0b */
62 {_CSTRUCT, offsetof(_cmsg, BC)},
63 /*0c */
64 {_CSTRUCT, offsetof(_cmsg, BChannelinformation)},
65 /*0d */
66 {_CMSTRUCT, offsetof(_cmsg, BProtocol)},
67 /*0e */
68 {_CSTRUCT, offsetof(_cmsg, CalledPartyNumber)},
69 /*0f */
70 {_CSTRUCT, offsetof(_cmsg, CalledPartySubaddress)},
71 /*10 */
72 {_CSTRUCT, offsetof(_cmsg, CallingPartyNumber)},
73 /*11 */
74 {_CSTRUCT, offsetof(_cmsg, CallingPartySubaddress)},
75 /*12 */
76 {_CDWORD, offsetof(_cmsg, CIPmask)},
77 /*13 */
78 {_CDWORD, offsetof(_cmsg, CIPmask2)},
79 /*14 */
80 {_CWORD, offsetof(_cmsg, CIPValue)},
81 /*15 */
82 {_CDWORD, offsetof(_cmsg, Class)},
83 /*16 */
84 {_CSTRUCT, offsetof(_cmsg, ConnectedNumber)},
85 /*17 */
86 {_CSTRUCT, offsetof(_cmsg, ConnectedSubaddress)},
87 /*18 */
88 {_CDWORD, offsetof(_cmsg, Data)},
89 /*19 */
90 {_CWORD, offsetof(_cmsg, DataHandle)},
91 /*1a */
92 {_CWORD, offsetof(_cmsg, DataLength)},
93 /*1b */
94 {_CSTRUCT, offsetof(_cmsg, FacilityConfirmationParameter)},
95 /*1c */
96 {_CSTRUCT, offsetof(_cmsg, Facilitydataarray)},
97 /*1d */
98 {_CSTRUCT, offsetof(_cmsg, FacilityIndicationParameter)},
99 /*1e */
100 {_CSTRUCT, offsetof(_cmsg, FacilityRequestParameter)},
101 /*1f */
102 {_CWORD, offsetof(_cmsg, FacilitySelector)},
103 /*20 */
104 {_CWORD, offsetof(_cmsg, Flags)},
105 /*21 */
106 {_CDWORD, offsetof(_cmsg, Function)},
107 /*22 */
108 {_CSTRUCT, offsetof(_cmsg, HLC)},
109 /*23 */
110 {_CWORD, offsetof(_cmsg, Info)},
111 /*24 */
112 {_CSTRUCT, offsetof(_cmsg, InfoElement)},
113 /*25 */
114 {_CDWORD, offsetof(_cmsg, InfoMask)},
115 /*26 */
116 {_CWORD, offsetof(_cmsg, InfoNumber)},
117 /*27 */
118 {_CSTRUCT, offsetof(_cmsg, Keypadfacility)},
119 /*28 */
120 {_CSTRUCT, offsetof(_cmsg, LLC)},
121 /*29 */
122 {_CSTRUCT, offsetof(_cmsg, ManuData)},
123 /*2a */
124 {_CDWORD, offsetof(_cmsg, ManuID)},
125 /*2b */
126 {_CSTRUCT, offsetof(_cmsg, NCPI)},
127 /*2c */
128 {_CWORD, offsetof(_cmsg, Reason)},
129 /*2d */
130 {_CWORD, offsetof(_cmsg, Reason_B3)},
131 /*2e */
132 {_CWORD, offsetof(_cmsg, Reject)},
133 /*2f */
134 {_CSTRUCT, offsetof(_cmsg, Useruserdata)}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
137static unsigned char *cpars[] =
138{
Joe Perches475be4d2012-02-19 19:52:38 -0800139 /* ALERT_REQ */ [0x01] = "\x03\x04\x0c\x27\x2f\x1c\x01\x01",
140 /* CONNECT_REQ */ [0x02] = "\x03\x14\x0e\x10\x0f\x11\x0d\x06\x08\x0a\x05\x07\x09\x01\x0b\x28\x22\x04\x0c\x27\x2f\x1c\x01\x01",
141 /* DISCONNECT_REQ */ [0x04] = "\x03\x04\x0c\x27\x2f\x1c\x01\x01",
142 /* LISTEN_REQ */ [0x05] = "\x03\x25\x12\x13\x10\x11\x01",
143 /* INFO_REQ */ [0x08] = "\x03\x0e\x04\x0c\x27\x2f\x1c\x01\x01",
144 /* FACILITY_REQ */ [0x09] = "\x03\x1f\x1e\x01",
145 /* SELECT_B_PROTOCOL_REQ */ [0x0a] = "\x03\x0d\x06\x08\x0a\x05\x07\x09\x01\x01",
146 /* CONNECT_B3_REQ */ [0x0b] = "\x03\x2b\x01",
147 /* DISCONNECT_B3_REQ */ [0x0d] = "\x03\x2b\x01",
148 /* DATA_B3_REQ */ [0x0f] = "\x03\x18\x1a\x19\x20\x01",
149 /* RESET_B3_REQ */ [0x10] = "\x03\x2b\x01",
150 /* ALERT_CONF */ [0x13] = "\x03\x23\x01",
151 /* CONNECT_CONF */ [0x14] = "\x03\x23\x01",
152 /* DISCONNECT_CONF */ [0x16] = "\x03\x23\x01",
153 /* LISTEN_CONF */ [0x17] = "\x03\x23\x01",
154 /* MANUFACTURER_REQ */ [0x18] = "\x03\x2a\x15\x21\x29\x01",
155 /* INFO_CONF */ [0x1a] = "\x03\x23\x01",
156 /* FACILITY_CONF */ [0x1b] = "\x03\x23\x1f\x1b\x01",
157 /* SELECT_B_PROTOCOL_CONF */ [0x1c] = "\x03\x23\x01",
158 /* CONNECT_B3_CONF */ [0x1d] = "\x03\x23\x01",
159 /* DISCONNECT_B3_CONF */ [0x1f] = "\x03\x23\x01",
160 /* DATA_B3_CONF */ [0x21] = "\x03\x19\x23\x01",
161 /* RESET_B3_CONF */ [0x22] = "\x03\x23\x01",
162 /* CONNECT_IND */ [0x26] = "\x03\x14\x0e\x10\x0f\x11\x0b\x28\x22\x04\x0c\x27\x2f\x1c\x01\x01",
163 /* CONNECT_ACTIVE_IND */ [0x27] = "\x03\x16\x17\x28\x01",
164 /* DISCONNECT_IND */ [0x28] = "\x03\x2c\x01",
165 /* MANUFACTURER_CONF */ [0x2a] = "\x03\x2a\x15\x21\x29\x01",
166 /* INFO_IND */ [0x2c] = "\x03\x26\x24\x01",
167 /* FACILITY_IND */ [0x2d] = "\x03\x1f\x1d\x01",
168 /* CONNECT_B3_IND */ [0x2f] = "\x03\x2b\x01",
169 /* CONNECT_B3_ACTIVE_IND */ [0x30] = "\x03\x2b\x01",
170 /* DISCONNECT_B3_IND */ [0x31] = "\x03\x2d\x2b\x01",
171 /* DATA_B3_IND */ [0x33] = "\x03\x18\x1a\x19\x20\x01",
172 /* RESET_B3_IND */ [0x34] = "\x03\x2b\x01",
173 /* CONNECT_B3_T90_ACTIVE_IND */ [0x35] = "\x03\x2b\x01",
174 /* CONNECT_RESP */ [0x38] = "\x03\x2e\x0d\x06\x08\x0a\x05\x07\x09\x01\x16\x17\x28\x04\x0c\x27\x2f\x1c\x01\x01",
175 /* CONNECT_ACTIVE_RESP */ [0x39] = "\x03\x01",
176 /* DISCONNECT_RESP */ [0x3a] = "\x03\x01",
177 /* MANUFACTURER_IND */ [0x3c] = "\x03\x2a\x15\x21\x29\x01",
178 /* INFO_RESP */ [0x3e] = "\x03\x01",
179 /* FACILITY_RESP */ [0x3f] = "\x03\x1f\x01",
180 /* CONNECT_B3_RESP */ [0x41] = "\x03\x2e\x2b\x01",
181 /* CONNECT_B3_ACTIVE_RESP */ [0x42] = "\x03\x01",
182 /* DISCONNECT_B3_RESP */ [0x43] = "\x03\x01",
183 /* DATA_B3_RESP */ [0x45] = "\x03\x19\x01",
184 /* RESET_B3_RESP */ [0x46] = "\x03\x01",
185 /* CONNECT_B3_T90_ACTIVE_RESP */ [0x47] = "\x03\x01",
186 /* MANUFACTURER_RESP */ [0x4e] = "\x03\x2a\x15\x21\x29\x01",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
189/*-------------------------------------------------------*/
190
Joe Perches475be4d2012-02-19 19:52:38 -0800191#define byteTLcpy(x, y) *(u8 *)(x) = *(u8 *)(y);
192#define wordTLcpy(x, y) *(u16 *)(x) = *(u16 *)(y);
193#define dwordTLcpy(x, y) memcpy(x, y, 4);
194#define structTLcpy(x, y, l) memcpy(x, y, l)
195#define structTLcpyovl(x, y, l) memmove(x, y, l)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Joe Perches475be4d2012-02-19 19:52:38 -0800197#define byteTRcpy(x, y) *(u8 *)(y) = *(u8 *)(x);
198#define wordTRcpy(x, y) *(u16 *)(y) = *(u16 *)(x);
199#define dwordTRcpy(x, y) memcpy(y, x, 4);
200#define structTRcpy(x, y, l) memcpy(y, x, l)
201#define structTRcpyovl(x, y, l) memmove(y, x, l)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203/*-------------------------------------------------------*/
204static unsigned command_2_index(unsigned c, unsigned sc)
205{
206 if (c & 0x80)
207 c = 0x9 + (c & 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 else if (c == 0x41)
209 c = 0x9 + 0x1;
Tilman Schmidt53622472014-10-11 13:46:30 +0200210 if (c > 0x18)
211 c = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return (sc & 3) * (0x9 + 0x9) + c;
213}
214
215/*-------------------------------------------------------*/
216#define TYP (cdef[cmsg->par[cmsg->p]].typ)
Joe Perches475be4d2012-02-19 19:52:38 -0800217#define OFF (((u8 *)cmsg) + cdef[cmsg->par[cmsg->p]].off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Joe Perches475be4d2012-02-19 19:52:38 -0800219static void jumpcstruct(_cmsg *cmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 unsigned layer;
222 for (cmsg->p++, layer = 1; layer;) {
223 /* $$$$$ assert (cmsg->p); */
224 cmsg->p++;
225 switch (TYP) {
226 case _CMSTRUCT:
227 layer++;
228 break;
229 case _CEND:
230 layer--;
231 break;
232 }
233 }
234}
235/*-------------------------------------------------------*/
Joe Perches475be4d2012-02-19 19:52:38 -0800236static void pars_2_message(_cmsg *cmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238
239 for (; TYP != _CEND; cmsg->p++) {
240 switch (TYP) {
241 case _CBYTE:
242 byteTLcpy(cmsg->m + cmsg->l, OFF);
243 cmsg->l++;
244 break;
245 case _CWORD:
246 wordTLcpy(cmsg->m + cmsg->l, OFF);
247 cmsg->l += 2;
248 break;
249 case _CDWORD:
250 dwordTLcpy(cmsg->m + cmsg->l, OFF);
251 cmsg->l += 4;
252 break;
253 case _CSTRUCT:
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700254 if (*(u8 **) OFF == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 *(cmsg->m + cmsg->l) = '\0';
256 cmsg->l++;
257 } else if (**(_cstruct *) OFF != 0xff) {
258 structTLcpy(cmsg->m + cmsg->l, *(_cstruct *) OFF, 1 + **(_cstruct *) OFF);
259 cmsg->l += 1 + **(_cstruct *) OFF;
260 } else {
261 _cstruct s = *(_cstruct *) OFF;
262 structTLcpy(cmsg->m + cmsg->l, s, 3 + *(u16 *) (s + 1));
263 cmsg->l += 3 + *(u16 *) (s + 1);
264 }
265 break;
266 case _CMSTRUCT:
267/*----- Metastruktur 0 -----*/
268 if (*(_cmstruct *) OFF == CAPI_DEFAULT) {
269 *(cmsg->m + cmsg->l) = '\0';
270 cmsg->l++;
271 jumpcstruct(cmsg);
272 }
273/*----- Metastruktur wird composed -----*/
274 else {
275 unsigned _l = cmsg->l;
276 unsigned _ls;
277 cmsg->l++;
278 cmsg->p++;
279 pars_2_message(cmsg);
280 _ls = cmsg->l - _l - 1;
281 if (_ls < 255)
282 (cmsg->m + _l)[0] = (u8) _ls;
283 else {
284 structTLcpyovl(cmsg->m + _l + 3, cmsg->m + _l + 1, _ls);
285 (cmsg->m + _l)[0] = 0xff;
286 wordTLcpy(cmsg->m + _l + 1, &_ls);
287 }
288 }
289 break;
290 }
291 }
292}
293
Tilman Schmidt4793d152009-06-07 09:09:23 +0000294/**
295 * capi_cmsg2message() - assemble CAPI 2.0 message from _cmsg structure
296 * @cmsg: _cmsg structure
297 * @msg: buffer for assembled message
298 *
299 * Return value: 0 for success
300 */
301
Joe Perches475be4d2012-02-19 19:52:38 -0800302unsigned capi_cmsg2message(_cmsg *cmsg, u8 *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 cmsg->m = msg;
305 cmsg->l = 8;
306 cmsg->p = 0;
307 cmsg->par = cpars[command_2_index(cmsg->Command, cmsg->Subcommand)];
308
309 pars_2_message(cmsg);
310
311 wordTLcpy(msg + 0, &cmsg->l);
312 byteTLcpy(cmsg->m + 4, &cmsg->Command);
313 byteTLcpy(cmsg->m + 5, &cmsg->Subcommand);
314 wordTLcpy(cmsg->m + 2, &cmsg->ApplId);
315 wordTLcpy(cmsg->m + 6, &cmsg->Messagenumber);
316
317 return 0;
318}
319
320/*-------------------------------------------------------*/
Joe Perches475be4d2012-02-19 19:52:38 -0800321static void message_2_pars(_cmsg *cmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 for (; TYP != _CEND; cmsg->p++) {
324
325 switch (TYP) {
326 case _CBYTE:
327 byteTRcpy(cmsg->m + cmsg->l, OFF);
328 cmsg->l++;
329 break;
330 case _CWORD:
331 wordTRcpy(cmsg->m + cmsg->l, OFF);
332 cmsg->l += 2;
333 break;
334 case _CDWORD:
335 dwordTRcpy(cmsg->m + cmsg->l, OFF);
336 cmsg->l += 4;
337 break;
338 case _CSTRUCT:
339 *(u8 **) OFF = cmsg->m + cmsg->l;
340
341 if (cmsg->m[cmsg->l] != 0xff)
342 cmsg->l += 1 + cmsg->m[cmsg->l];
343 else
344 cmsg->l += 3 + *(u16 *) (cmsg->m + cmsg->l + 1);
345 break;
346 case _CMSTRUCT:
347/*----- Metastruktur 0 -----*/
348 if (cmsg->m[cmsg->l] == '\0') {
349 *(_cmstruct *) OFF = CAPI_DEFAULT;
350 cmsg->l++;
351 jumpcstruct(cmsg);
352 } else {
353 unsigned _l = cmsg->l;
354 *(_cmstruct *) OFF = CAPI_COMPOSE;
355 cmsg->l = (cmsg->m + _l)[0] == 255 ? cmsg->l + 3 : cmsg->l + 1;
356 cmsg->p++;
357 message_2_pars(cmsg);
358 }
359 break;
360 }
361 }
362}
363
Tilman Schmidt4793d152009-06-07 09:09:23 +0000364/**
365 * capi_message2cmsg() - disassemble CAPI 2.0 message into _cmsg structure
366 * @cmsg: _cmsg structure
367 * @msg: buffer for assembled message
368 *
369 * Return value: 0 for success
370 */
371
Joe Perches475be4d2012-02-19 19:52:38 -0800372unsigned capi_message2cmsg(_cmsg *cmsg, u8 *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 memset(cmsg, 0, sizeof(_cmsg));
375 cmsg->m = msg;
376 cmsg->l = 8;
377 cmsg->p = 0;
378 byteTRcpy(cmsg->m + 4, &cmsg->Command);
379 byteTRcpy(cmsg->m + 5, &cmsg->Subcommand);
380 cmsg->par = cpars[command_2_index(cmsg->Command, cmsg->Subcommand)];
381
382 message_2_pars(cmsg);
383
384 wordTRcpy(msg + 0, &cmsg->l);
385 wordTRcpy(cmsg->m + 2, &cmsg->ApplId);
386 wordTRcpy(cmsg->m + 6, &cmsg->Messagenumber);
387
388 return 0;
389}
390
Tilman Schmidt4793d152009-06-07 09:09:23 +0000391/**
392 * capi_cmsg_header() - initialize header part of _cmsg structure
393 * @cmsg: _cmsg structure
394 * @_ApplId: ApplID field value
395 * @_Command: Command field value
396 * @_Subcommand: Subcommand field value
397 * @_Messagenumber: Message Number field value
398 * @_Controller: Controller/PLCI/NCCI field value
399 *
400 * Return value: 0 for success
401 */
402
Joe Perches475be4d2012-02-19 19:52:38 -0800403unsigned capi_cmsg_header(_cmsg *cmsg, u16 _ApplId,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 u8 _Command, u8 _Subcommand,
405 u16 _Messagenumber, u32 _Controller)
406{
407 memset(cmsg, 0, sizeof(_cmsg));
408 cmsg->ApplId = _ApplId;
409 cmsg->Command = _Command;
410 cmsg->Subcommand = _Subcommand;
411 cmsg->Messagenumber = _Messagenumber;
412 cmsg->adr.adrController = _Controller;
413 return 0;
414}
415
416/*-------------------------------------------------------*/
417
418static char *mnames[] =
419{
420 [0x01] = "ALERT_REQ",
421 [0x02] = "CONNECT_REQ",
422 [0x04] = "DISCONNECT_REQ",
423 [0x05] = "LISTEN_REQ",
424 [0x08] = "INFO_REQ",
425 [0x09] = "FACILITY_REQ",
426 [0x0a] = "SELECT_B_PROTOCOL_REQ",
427 [0x0b] = "CONNECT_B3_REQ",
428 [0x0d] = "DISCONNECT_B3_REQ",
429 [0x0f] = "DATA_B3_REQ",
430 [0x10] = "RESET_B3_REQ",
431 [0x13] = "ALERT_CONF",
432 [0x14] = "CONNECT_CONF",
433 [0x16] = "DISCONNECT_CONF",
434 [0x17] = "LISTEN_CONF",
435 [0x18] = "MANUFACTURER_REQ",
436 [0x1a] = "INFO_CONF",
437 [0x1b] = "FACILITY_CONF",
438 [0x1c] = "SELECT_B_PROTOCOL_CONF",
439 [0x1d] = "CONNECT_B3_CONF",
440 [0x1f] = "DISCONNECT_B3_CONF",
441 [0x21] = "DATA_B3_CONF",
442 [0x22] = "RESET_B3_CONF",
443 [0x26] = "CONNECT_IND",
444 [0x27] = "CONNECT_ACTIVE_IND",
445 [0x28] = "DISCONNECT_IND",
446 [0x2a] = "MANUFACTURER_CONF",
447 [0x2c] = "INFO_IND",
448 [0x2d] = "FACILITY_IND",
449 [0x2f] = "CONNECT_B3_IND",
450 [0x30] = "CONNECT_B3_ACTIVE_IND",
451 [0x31] = "DISCONNECT_B3_IND",
452 [0x33] = "DATA_B3_IND",
453 [0x34] = "RESET_B3_IND",
454 [0x35] = "CONNECT_B3_T90_ACTIVE_IND",
455 [0x38] = "CONNECT_RESP",
456 [0x39] = "CONNECT_ACTIVE_RESP",
457 [0x3a] = "DISCONNECT_RESP",
458 [0x3c] = "MANUFACTURER_IND",
459 [0x3e] = "INFO_RESP",
460 [0x3f] = "FACILITY_RESP",
461 [0x41] = "CONNECT_B3_RESP",
462 [0x42] = "CONNECT_B3_ACTIVE_RESP",
463 [0x43] = "DISCONNECT_B3_RESP",
464 [0x45] = "DATA_B3_RESP",
465 [0x46] = "RESET_B3_RESP",
466 [0x47] = "CONNECT_B3_T90_ACTIVE_RESP",
467 [0x4e] = "MANUFACTURER_RESP"
468};
469
Tilman Schmidt4793d152009-06-07 09:09:23 +0000470/**
471 * capi_cmd2str() - convert CAPI 2.0 command/subcommand number to name
472 * @cmd: command number
473 * @subcmd: subcommand number
474 *
475 * Return value: static string, NULL if command/subcommand unknown
476 */
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478char *capi_cmd2str(u8 cmd, u8 subcmd)
479{
480 return mnames[command_2_index(cmd, subcmd)];
481}
482
483
484/*-------------------------------------------------------*/
Karsten Keil17f0cd22007-02-28 20:13:50 -0800485
486#ifdef CONFIG_CAPI_TRACE
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488/*-------------------------------------------------------*/
489
490static char *pnames[] =
491{
Joe Perches475be4d2012-02-19 19:52:38 -0800492 /*00 */ NULL,
493 /*01 */ NULL,
494 /*02 */ NULL,
495 /*03 */ "Controller/PLCI/NCCI",
496 /*04 */ "AdditionalInfo",
497 /*05 */ "B1configuration",
498 /*06 */ "B1protocol",
499 /*07 */ "B2configuration",
500 /*08 */ "B2protocol",
501 /*09 */ "B3configuration",
502 /*0a */ "B3protocol",
503 /*0b */ "BC",
504 /*0c */ "BChannelinformation",
505 /*0d */ "BProtocol",
506 /*0e */ "CalledPartyNumber",
507 /*0f */ "CalledPartySubaddress",
508 /*10 */ "CallingPartyNumber",
509 /*11 */ "CallingPartySubaddress",
510 /*12 */ "CIPmask",
511 /*13 */ "CIPmask2",
512 /*14 */ "CIPValue",
513 /*15 */ "Class",
514 /*16 */ "ConnectedNumber",
515 /*17 */ "ConnectedSubaddress",
516 /*18 */ "Data32",
517 /*19 */ "DataHandle",
518 /*1a */ "DataLength",
519 /*1b */ "FacilityConfirmationParameter",
520 /*1c */ "Facilitydataarray",
521 /*1d */ "FacilityIndicationParameter",
522 /*1e */ "FacilityRequestParameter",
523 /*1f */ "FacilitySelector",
524 /*20 */ "Flags",
525 /*21 */ "Function",
526 /*22 */ "HLC",
527 /*23 */ "Info",
528 /*24 */ "InfoElement",
529 /*25 */ "InfoMask",
530 /*26 */ "InfoNumber",
531 /*27 */ "Keypadfacility",
532 /*28 */ "LLC",
533 /*29 */ "ManuData",
534 /*2a */ "ManuID",
535 /*2b */ "NCPI",
536 /*2c */ "Reason",
537 /*2d */ "Reason_B3",
538 /*2e */ "Reject",
539 /*2f */ "Useruserdata"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540};
541
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544#include <stdarg.h>
545
546/*-------------------------------------------------------*/
Joe Perches475be4d2012-02-19 19:52:38 -0800547static _cdebbuf *bufprint(_cdebbuf *cdb, char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 va_list f;
Joe Perches475be4d2012-02-19 19:52:38 -0800550 size_t n, r;
Karsten Keil17f0cd22007-02-28 20:13:50 -0800551
552 if (!cdb)
553 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 va_start(f, fmt);
Karsten Keil17f0cd22007-02-28 20:13:50 -0800555 r = cdb->size - cdb->pos;
556 n = vsnprintf(cdb->p, r, fmt, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 va_end(f);
Karsten Keil17f0cd22007-02-28 20:13:50 -0800558 if (n >= r) {
559 /* truncated, need bigger buffer */
560 size_t ns = 2 * cdb->size;
561 u_char *nb;
562
563 while ((ns - cdb->pos) <= n)
564 ns *= 2;
565 nb = kmalloc(ns, GFP_ATOMIC);
566 if (!nb) {
567 cdebbuf_free(cdb);
568 return NULL;
569 }
570 memcpy(nb, cdb->buf, cdb->pos);
571 kfree(cdb->buf);
572 nb[cdb->pos] = 0;
573 cdb->buf = nb;
574 cdb->p = cdb->buf + cdb->pos;
575 cdb->size = ns;
576 va_start(f, fmt);
577 r = cdb->size - cdb->pos;
578 n = vsnprintf(cdb->p, r, fmt, f);
579 va_end(f);
580 }
581 cdb->p += n;
582 cdb->pos += n;
583 return cdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Joe Perches475be4d2012-02-19 19:52:38 -0800586static _cdebbuf *printstructlen(_cdebbuf *cdb, u8 *m, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 unsigned hex = 0;
Karsten Keil17f0cd22007-02-28 20:13:50 -0800589
590 if (!cdb)
591 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 for (; len; len--, m++)
593 if (isalnum(*m) || *m == ' ') {
594 if (hex)
Karsten Keil17f0cd22007-02-28 20:13:50 -0800595 cdb = bufprint(cdb, ">");
596 cdb = bufprint(cdb, "%c", *m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 hex = 0;
598 } else {
599 if (!hex)
Karsten Keil17f0cd22007-02-28 20:13:50 -0800600 cdb = bufprint(cdb, "<%02x", *m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 else
Karsten Keil17f0cd22007-02-28 20:13:50 -0800602 cdb = bufprint(cdb, " %02x", *m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 hex = 1;
604 }
605 if (hex)
Karsten Keil17f0cd22007-02-28 20:13:50 -0800606 cdb = bufprint(cdb, ">");
607 return cdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Joe Perches475be4d2012-02-19 19:52:38 -0800610static _cdebbuf *printstruct(_cdebbuf *cdb, u8 *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 unsigned len;
Karsten Keil17f0cd22007-02-28 20:13:50 -0800613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (m[0] != 0xff) {
615 len = m[0];
616 m += 1;
617 } else {
618 len = ((u16 *) (m + 1))[0];
619 m += 3;
620 }
Karsten Keil17f0cd22007-02-28 20:13:50 -0800621 cdb = printstructlen(cdb, m, len);
622 return cdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
625/*-------------------------------------------------------*/
626#define NAME (pnames[cmsg->par[cmsg->p]])
627
Karsten Keil17f0cd22007-02-28 20:13:50 -0800628static _cdebbuf *protocol_message_2_pars(_cdebbuf *cdb, _cmsg *cmsg, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 for (; TYP != _CEND; cmsg->p++) {
631 int slen = 29 + 3 - level;
632 int i;
633
Karsten Keil17f0cd22007-02-28 20:13:50 -0800634 if (!cdb)
635 return NULL;
636 cdb = bufprint(cdb, " ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 for (i = 0; i < level - 1; i++)
Karsten Keil17f0cd22007-02-28 20:13:50 -0800638 cdb = bufprint(cdb, " ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 switch (TYP) {
641 case _CBYTE:
Karsten Keil17f0cd22007-02-28 20:13:50 -0800642 cdb = bufprint(cdb, "%-*s = 0x%x\n", slen, NAME, *(u8 *) (cmsg->m + cmsg->l));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 cmsg->l++;
644 break;
645 case _CWORD:
Karsten Keil17f0cd22007-02-28 20:13:50 -0800646 cdb = bufprint(cdb, "%-*s = 0x%x\n", slen, NAME, *(u16 *) (cmsg->m + cmsg->l));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 cmsg->l += 2;
648 break;
649 case _CDWORD:
Karsten Keil17f0cd22007-02-28 20:13:50 -0800650 cdb = bufprint(cdb, "%-*s = 0x%lx\n", slen, NAME, *(u32 *) (cmsg->m + cmsg->l));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 cmsg->l += 4;
652 break;
653 case _CSTRUCT:
Karsten Keil17f0cd22007-02-28 20:13:50 -0800654 cdb = bufprint(cdb, "%-*s = ", slen, NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (cmsg->m[cmsg->l] == '\0')
Karsten Keil17f0cd22007-02-28 20:13:50 -0800656 cdb = bufprint(cdb, "default");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 else
Karsten Keil17f0cd22007-02-28 20:13:50 -0800658 cdb = printstruct(cdb, cmsg->m + cmsg->l);
659 cdb = bufprint(cdb, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (cmsg->m[cmsg->l] != 0xff)
661 cmsg->l += 1 + cmsg->m[cmsg->l];
662 else
663 cmsg->l += 3 + *(u16 *) (cmsg->m + cmsg->l + 1);
664
665 break;
666
667 case _CMSTRUCT:
668/*----- Metastruktur 0 -----*/
669 if (cmsg->m[cmsg->l] == '\0') {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800670 cdb = bufprint(cdb, "%-*s = default\n", slen, NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 cmsg->l++;
672 jumpcstruct(cmsg);
673 } else {
674 char *name = NAME;
675 unsigned _l = cmsg->l;
Karsten Keil17f0cd22007-02-28 20:13:50 -0800676 cdb = bufprint(cdb, "%-*s\n", slen, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 cmsg->l = (cmsg->m + _l)[0] == 255 ? cmsg->l + 3 : cmsg->l + 1;
678 cmsg->p++;
Karsten Keil17f0cd22007-02-28 20:13:50 -0800679 cdb = protocol_message_2_pars(cdb, cmsg, level + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681 break;
682 }
683 }
Karsten Keil17f0cd22007-02-28 20:13:50 -0800684 return cdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686/*-------------------------------------------------------*/
Karsten Keil17f0cd22007-02-28 20:13:50 -0800687
688static _cdebbuf *g_debbuf;
689static u_long g_debbuf_lock;
690static _cmsg *g_cmsg;
691
Adrian Bunk3a3a51d2007-05-08 00:32:33 -0700692static _cdebbuf *cdebbuf_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Karsten Keil17f0cd22007-02-28 20:13:50 -0800694 _cdebbuf *cdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Karsten Keil17f0cd22007-02-28 20:13:50 -0800696 if (likely(!test_and_set_bit(1, &g_debbuf_lock))) {
697 cdb = g_debbuf;
698 goto init;
699 } else
700 cdb = kmalloc(sizeof(_cdebbuf), GFP_ATOMIC);
701 if (!cdb)
702 return NULL;
703 cdb->buf = kmalloc(CDEBUG_SIZE, GFP_ATOMIC);
704 if (!cdb->buf) {
705 kfree(cdb);
706 return NULL;
707 }
708 cdb->size = CDEBUG_SIZE;
709init:
710 cdb->buf[0] = 0;
711 cdb->p = cdb->buf;
712 cdb->pos = 0;
713 return cdb;
714}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Tilman Schmidt4793d152009-06-07 09:09:23 +0000716/**
717 * cdebbuf_free() - free CAPI debug buffer
718 * @cdb: buffer to free
719 */
720
Karsten Keil17f0cd22007-02-28 20:13:50 -0800721void cdebbuf_free(_cdebbuf *cdb)
722{
723 if (likely(cdb == g_debbuf)) {
724 test_and_clear_bit(1, &g_debbuf_lock);
725 return;
726 }
727 if (likely(cdb))
728 kfree(cdb->buf);
729 kfree(cdb);
730}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Karsten Keil17f0cd22007-02-28 20:13:50 -0800732
Tilman Schmidt4793d152009-06-07 09:09:23 +0000733/**
734 * capi_message2str() - format CAPI 2.0 message for printing
735 * @msg: CAPI 2.0 message
736 *
737 * Allocates a CAPI debug buffer and fills it with a printable representation
738 * of the CAPI 2.0 message in @msg.
739 * Return value: allocated debug buffer, NULL on error
740 * The returned buffer should be freed by a call to cdebbuf_free() after use.
741 */
742
Joe Perches475be4d2012-02-19 19:52:38 -0800743_cdebbuf *capi_message2str(u8 *msg)
Karsten Keil17f0cd22007-02-28 20:13:50 -0800744{
745 _cdebbuf *cdb;
746 _cmsg *cmsg;
747
748 cdb = cdebbuf_alloc();
749 if (unlikely(!cdb))
750 return NULL;
751 if (likely(cdb == g_debbuf))
752 cmsg = g_cmsg;
753 else
754 cmsg = kmalloc(sizeof(_cmsg), GFP_ATOMIC);
755 if (unlikely(!cmsg)) {
756 cdebbuf_free(cdb);
757 return NULL;
758 }
759 cmsg->m = msg;
760 cmsg->l = 8;
761 cmsg->p = 0;
762 byteTRcpy(cmsg->m + 4, &cmsg->Command);
763 byteTRcpy(cmsg->m + 5, &cmsg->Subcommand);
764 cmsg->par = cpars[command_2_index(cmsg->Command, cmsg->Subcommand)];
765
766 cdb = bufprint(cdb, "%-26s ID=%03d #0x%04x LEN=%04d\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800767 mnames[command_2_index(cmsg->Command, cmsg->Subcommand)],
768 ((unsigned short *) msg)[1],
769 ((unsigned short *) msg)[3],
770 ((unsigned short *) msg)[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Karsten Keil17f0cd22007-02-28 20:13:50 -0800772 cdb = protocol_message_2_pars(cdb, cmsg, 1);
773 if (unlikely(cmsg != g_cmsg))
774 kfree(cmsg);
775 return cdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
Tilman Schmidt4793d152009-06-07 09:09:23 +0000778/**
779 * capi_cmsg2str() - format _cmsg structure for printing
780 * @cmsg: _cmsg structure
781 *
782 * Allocates a CAPI debug buffer and fills it with a printable representation
783 * of the CAPI 2.0 message stored in @cmsg by a previous call to
784 * capi_cmsg2message() or capi_message2cmsg().
785 * Return value: allocated debug buffer, NULL on error
786 * The returned buffer should be freed by a call to cdebbuf_free() after use.
787 */
788
Joe Perches475be4d2012-02-19 19:52:38 -0800789_cdebbuf *capi_cmsg2str(_cmsg *cmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Karsten Keil17f0cd22007-02-28 20:13:50 -0800791 _cdebbuf *cdb;
792
Tilman Schmidt7d31acd2009-06-07 09:09:24 +0000793 if (!cmsg->m)
794 return NULL; /* no message */
Karsten Keil17f0cd22007-02-28 20:13:50 -0800795 cdb = cdebbuf_alloc();
796 if (!cdb)
797 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 cmsg->l = 8;
799 cmsg->p = 0;
Karsten Keil17f0cd22007-02-28 20:13:50 -0800800 cdb = bufprint(cdb, "%s ID=%03d #0x%04x LEN=%04d\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800801 mnames[command_2_index(cmsg->Command, cmsg->Subcommand)],
802 ((u16 *) cmsg->m)[1],
803 ((u16 *) cmsg->m)[3],
804 ((u16 *) cmsg->m)[0]);
Karsten Keil17f0cd22007-02-28 20:13:50 -0800805 cdb = protocol_message_2_pars(cdb, cmsg, 1);
806 return cdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
Karsten Keil17f0cd22007-02-28 20:13:50 -0800809int __init cdebug_init(void)
810{
Joe Perches475be4d2012-02-19 19:52:38 -0800811 g_cmsg = kmalloc(sizeof(_cmsg), GFP_KERNEL);
Karsten Keil17f0cd22007-02-28 20:13:50 -0800812 if (!g_cmsg)
Marcin Slusarz066b2112008-05-14 23:30:06 -0700813 return -ENOMEM;
Karsten Keil17f0cd22007-02-28 20:13:50 -0800814 g_debbuf = kmalloc(sizeof(_cdebbuf), GFP_KERNEL);
815 if (!g_debbuf) {
816 kfree(g_cmsg);
Marcin Slusarz066b2112008-05-14 23:30:06 -0700817 return -ENOMEM;
Karsten Keil17f0cd22007-02-28 20:13:50 -0800818 }
819 g_debbuf->buf = kmalloc(CDEBUG_GSIZE, GFP_KERNEL);
820 if (!g_debbuf->buf) {
821 kfree(g_cmsg);
822 kfree(g_debbuf);
Joe Perchesa419aef2009-08-18 11:18:35 -0700823 return -ENOMEM;
Karsten Keil17f0cd22007-02-28 20:13:50 -0800824 }
825 g_debbuf->size = CDEBUG_GSIZE;
826 g_debbuf->buf[0] = 0;
827 g_debbuf->p = g_debbuf->buf;
828 g_debbuf->pos = 0;
829 return 0;
830}
831
832void __exit cdebug_exit(void)
833{
834 if (g_debbuf)
835 kfree(g_debbuf->buf);
836 kfree(g_debbuf);
837 kfree(g_cmsg);
838}
839
840#else /* !CONFIG_CAPI_TRACE */
841
842static _cdebbuf g_debbuf = {"CONFIG_CAPI_TRACE not enabled", NULL, 0, 0};
843
Joe Perches475be4d2012-02-19 19:52:38 -0800844_cdebbuf *capi_message2str(u8 *msg)
Karsten Keil17f0cd22007-02-28 20:13:50 -0800845{
846 return &g_debbuf;
847}
848
Joe Perches475be4d2012-02-19 19:52:38 -0800849_cdebbuf *capi_cmsg2str(_cmsg *cmsg)
Karsten Keil17f0cd22007-02-28 20:13:50 -0800850{
851 return &g_debbuf;
852}
853
Karsten Keil17f0cd22007-02-28 20:13:50 -0800854void cdebbuf_free(_cdebbuf *cdb)
855{
856}
857
858int __init cdebug_init(void)
859{
860 return 0;
861}
862
863void __exit cdebug_exit(void)
864{
865}
866
867#endif
868
Karsten Keil17f0cd22007-02-28 20:13:50 -0800869EXPORT_SYMBOL(cdebbuf_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870EXPORT_SYMBOL(capi_cmsg2message);
871EXPORT_SYMBOL(capi_message2cmsg);
872EXPORT_SYMBOL(capi_cmsg_header);
873EXPORT_SYMBOL(capi_cmd2str);
874EXPORT_SYMBOL(capi_cmsg2str);
875EXPORT_SYMBOL(capi_message2str);