]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/isdn/capi/capiutil.c
Merge tag 'scsi-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb...
[karo-tx-linux.git] / drivers / isdn / capi / capiutil.c
1 /* $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>
20 #include <linux/isdn/capiutil.h>
21 #include <linux/slab.h>
22
23 /* from CAPI2.0 DDK AVM Berlin GmbH */
24
25 typedef 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
37 static _cdef cdef[] =
38 {
39         /*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)}
135 };
136
137 static unsigned char *cpars[] =
138 {
139         /* 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",
187 };
188
189 /*-------------------------------------------------------*/
190
191 #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)
196
197 #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)
202
203 /*-------------------------------------------------------*/
204 static unsigned command_2_index(unsigned c, unsigned sc)
205 {
206         if (c & 0x80)
207                 c = 0x9 + (c & 0x0f);
208         else if (c <= 0x0f);
209         else if (c == 0x41)
210                 c = 0x9 + 0x1;
211         else if (c == 0xff)
212                 c = 0x00;
213         return (sc & 3) * (0x9 + 0x9) + c;
214 }
215
216 /*-------------------------------------------------------*/
217 #define TYP (cdef[cmsg->par[cmsg->p]].typ)
218 #define OFF (((u8 *)cmsg) + cdef[cmsg->par[cmsg->p]].off)
219
220 static void jumpcstruct(_cmsg *cmsg)
221 {
222         unsigned layer;
223         for (cmsg->p++, layer = 1; layer;) {
224                 /* $$$$$ assert (cmsg->p); */
225                 cmsg->p++;
226                 switch (TYP) {
227                 case _CMSTRUCT:
228                         layer++;
229                         break;
230                 case _CEND:
231                         layer--;
232                         break;
233                 }
234         }
235 }
236 /*-------------------------------------------------------*/
237 static void pars_2_message(_cmsg *cmsg)
238 {
239
240         for (; TYP != _CEND; cmsg->p++) {
241                 switch (TYP) {
242                 case _CBYTE:
243                         byteTLcpy(cmsg->m + cmsg->l, OFF);
244                         cmsg->l++;
245                         break;
246                 case _CWORD:
247                         wordTLcpy(cmsg->m + cmsg->l, OFF);
248                         cmsg->l += 2;
249                         break;
250                 case _CDWORD:
251                         dwordTLcpy(cmsg->m + cmsg->l, OFF);
252                         cmsg->l += 4;
253                         break;
254                 case _CSTRUCT:
255                         if (*(u8 **) OFF == NULL) {
256                                 *(cmsg->m + cmsg->l) = '\0';
257                                 cmsg->l++;
258                         } else if (**(_cstruct *) OFF != 0xff) {
259                                 structTLcpy(cmsg->m + cmsg->l, *(_cstruct *) OFF, 1 + **(_cstruct *) OFF);
260                                 cmsg->l += 1 + **(_cstruct *) OFF;
261                         } else {
262                                 _cstruct s = *(_cstruct *) OFF;
263                                 structTLcpy(cmsg->m + cmsg->l, s, 3 + *(u16 *) (s + 1));
264                                 cmsg->l += 3 + *(u16 *) (s + 1);
265                         }
266                         break;
267                 case _CMSTRUCT:
268 /*----- Metastruktur 0 -----*/
269                         if (*(_cmstruct *) OFF == CAPI_DEFAULT) {
270                                 *(cmsg->m + cmsg->l) = '\0';
271                                 cmsg->l++;
272                                 jumpcstruct(cmsg);
273                         }
274 /*----- Metastruktur wird composed -----*/
275                         else {
276                                 unsigned _l = cmsg->l;
277                                 unsigned _ls;
278                                 cmsg->l++;
279                                 cmsg->p++;
280                                 pars_2_message(cmsg);
281                                 _ls = cmsg->l - _l - 1;
282                                 if (_ls < 255)
283                                         (cmsg->m + _l)[0] = (u8) _ls;
284                                 else {
285                                         structTLcpyovl(cmsg->m + _l + 3, cmsg->m + _l + 1, _ls);
286                                         (cmsg->m + _l)[0] = 0xff;
287                                         wordTLcpy(cmsg->m + _l + 1, &_ls);
288                                 }
289                         }
290                         break;
291                 }
292         }
293 }
294
295 /**
296  * capi_cmsg2message() - assemble CAPI 2.0 message from _cmsg structure
297  * @cmsg:       _cmsg structure
298  * @msg:        buffer for assembled message
299  *
300  * Return value: 0 for success
301  */
302
303 unsigned capi_cmsg2message(_cmsg *cmsg, u8 *msg)
304 {
305         cmsg->m = msg;
306         cmsg->l = 8;
307         cmsg->p = 0;
308         cmsg->par = cpars[command_2_index(cmsg->Command, cmsg->Subcommand)];
309
310         pars_2_message(cmsg);
311
312         wordTLcpy(msg + 0, &cmsg->l);
313         byteTLcpy(cmsg->m + 4, &cmsg->Command);
314         byteTLcpy(cmsg->m + 5, &cmsg->Subcommand);
315         wordTLcpy(cmsg->m + 2, &cmsg->ApplId);
316         wordTLcpy(cmsg->m + 6, &cmsg->Messagenumber);
317
318         return 0;
319 }
320
321 /*-------------------------------------------------------*/
322 static void message_2_pars(_cmsg *cmsg)
323 {
324         for (; TYP != _CEND; cmsg->p++) {
325
326                 switch (TYP) {
327                 case _CBYTE:
328                         byteTRcpy(cmsg->m + cmsg->l, OFF);
329                         cmsg->l++;
330                         break;
331                 case _CWORD:
332                         wordTRcpy(cmsg->m + cmsg->l, OFF);
333                         cmsg->l += 2;
334                         break;
335                 case _CDWORD:
336                         dwordTRcpy(cmsg->m + cmsg->l, OFF);
337                         cmsg->l += 4;
338                         break;
339                 case _CSTRUCT:
340                         *(u8 **) OFF = cmsg->m + cmsg->l;
341
342                         if (cmsg->m[cmsg->l] != 0xff)
343                                 cmsg->l += 1 + cmsg->m[cmsg->l];
344                         else
345                                 cmsg->l += 3 + *(u16 *) (cmsg->m + cmsg->l + 1);
346                         break;
347                 case _CMSTRUCT:
348 /*----- Metastruktur 0 -----*/
349                         if (cmsg->m[cmsg->l] == '\0') {
350                                 *(_cmstruct *) OFF = CAPI_DEFAULT;
351                                 cmsg->l++;
352                                 jumpcstruct(cmsg);
353                         } else {
354                                 unsigned _l = cmsg->l;
355                                 *(_cmstruct *) OFF = CAPI_COMPOSE;
356                                 cmsg->l = (cmsg->m + _l)[0] == 255 ? cmsg->l + 3 : cmsg->l + 1;
357                                 cmsg->p++;
358                                 message_2_pars(cmsg);
359                         }
360                         break;
361                 }
362         }
363 }
364
365 /**
366  * capi_message2cmsg() - disassemble CAPI 2.0 message into _cmsg structure
367  * @cmsg:       _cmsg structure
368  * @msg:        buffer for assembled message
369  *
370  * Return value: 0 for success
371  */
372
373 unsigned capi_message2cmsg(_cmsg *cmsg, u8 *msg)
374 {
375         memset(cmsg, 0, sizeof(_cmsg));
376         cmsg->m = msg;
377         cmsg->l = 8;
378         cmsg->p = 0;
379         byteTRcpy(cmsg->m + 4, &cmsg->Command);
380         byteTRcpy(cmsg->m + 5, &cmsg->Subcommand);
381         cmsg->par = cpars[command_2_index(cmsg->Command, cmsg->Subcommand)];
382
383         message_2_pars(cmsg);
384
385         wordTRcpy(msg + 0, &cmsg->l);
386         wordTRcpy(cmsg->m + 2, &cmsg->ApplId);
387         wordTRcpy(cmsg->m + 6, &cmsg->Messagenumber);
388
389         return 0;
390 }
391
392 /**
393  * capi_cmsg_header() - initialize header part of _cmsg structure
394  * @cmsg:       _cmsg structure
395  * @_ApplId:    ApplID field value
396  * @_Command:   Command field value
397  * @_Subcommand:        Subcommand field value
398  * @_Messagenumber:     Message Number field value
399  * @_Controller:        Controller/PLCI/NCCI field value
400  *
401  * Return value: 0 for success
402  */
403
404 unsigned capi_cmsg_header(_cmsg *cmsg, u16 _ApplId,
405                           u8 _Command, u8 _Subcommand,
406                           u16 _Messagenumber, u32 _Controller)
407 {
408         memset(cmsg, 0, sizeof(_cmsg));
409         cmsg->ApplId = _ApplId;
410         cmsg->Command = _Command;
411         cmsg->Subcommand = _Subcommand;
412         cmsg->Messagenumber = _Messagenumber;
413         cmsg->adr.adrController = _Controller;
414         return 0;
415 }
416
417 /*-------------------------------------------------------*/
418
419 static char *mnames[] =
420 {
421         [0x01] = "ALERT_REQ",
422         [0x02] = "CONNECT_REQ",
423         [0x04] = "DISCONNECT_REQ",
424         [0x05] = "LISTEN_REQ",
425         [0x08] = "INFO_REQ",
426         [0x09] = "FACILITY_REQ",
427         [0x0a] = "SELECT_B_PROTOCOL_REQ",
428         [0x0b] = "CONNECT_B3_REQ",
429         [0x0d] = "DISCONNECT_B3_REQ",
430         [0x0f] = "DATA_B3_REQ",
431         [0x10] = "RESET_B3_REQ",
432         [0x13] = "ALERT_CONF",
433         [0x14] = "CONNECT_CONF",
434         [0x16] = "DISCONNECT_CONF",
435         [0x17] = "LISTEN_CONF",
436         [0x18] = "MANUFACTURER_REQ",
437         [0x1a] = "INFO_CONF",
438         [0x1b] = "FACILITY_CONF",
439         [0x1c] = "SELECT_B_PROTOCOL_CONF",
440         [0x1d] = "CONNECT_B3_CONF",
441         [0x1f] = "DISCONNECT_B3_CONF",
442         [0x21] = "DATA_B3_CONF",
443         [0x22] = "RESET_B3_CONF",
444         [0x26] = "CONNECT_IND",
445         [0x27] = "CONNECT_ACTIVE_IND",
446         [0x28] = "DISCONNECT_IND",
447         [0x2a] = "MANUFACTURER_CONF",
448         [0x2c] = "INFO_IND",
449         [0x2d] = "FACILITY_IND",
450         [0x2f] = "CONNECT_B3_IND",
451         [0x30] = "CONNECT_B3_ACTIVE_IND",
452         [0x31] = "DISCONNECT_B3_IND",
453         [0x33] = "DATA_B3_IND",
454         [0x34] = "RESET_B3_IND",
455         [0x35] = "CONNECT_B3_T90_ACTIVE_IND",
456         [0x38] = "CONNECT_RESP",
457         [0x39] = "CONNECT_ACTIVE_RESP",
458         [0x3a] = "DISCONNECT_RESP",
459         [0x3c] = "MANUFACTURER_IND",
460         [0x3e] = "INFO_RESP",
461         [0x3f] = "FACILITY_RESP",
462         [0x41] = "CONNECT_B3_RESP",
463         [0x42] = "CONNECT_B3_ACTIVE_RESP",
464         [0x43] = "DISCONNECT_B3_RESP",
465         [0x45] = "DATA_B3_RESP",
466         [0x46] = "RESET_B3_RESP",
467         [0x47] = "CONNECT_B3_T90_ACTIVE_RESP",
468         [0x4e] = "MANUFACTURER_RESP"
469 };
470
471 /**
472  * capi_cmd2str() - convert CAPI 2.0 command/subcommand number to name
473  * @cmd:        command number
474  * @subcmd:     subcommand number
475  *
476  * Return value: static string, NULL if command/subcommand unknown
477  */
478
479 char *capi_cmd2str(u8 cmd, u8 subcmd)
480 {
481         return mnames[command_2_index(cmd, subcmd)];
482 }
483
484
485 /*-------------------------------------------------------*/
486
487 #ifdef CONFIG_CAPI_TRACE
488
489 /*-------------------------------------------------------*/
490
491 static char *pnames[] =
492 {
493         /*00 */ NULL,
494         /*01 */ NULL,
495         /*02 */ NULL,
496         /*03 */ "Controller/PLCI/NCCI",
497         /*04 */ "AdditionalInfo",
498         /*05 */ "B1configuration",
499         /*06 */ "B1protocol",
500         /*07 */ "B2configuration",
501         /*08 */ "B2protocol",
502         /*09 */ "B3configuration",
503         /*0a */ "B3protocol",
504         /*0b */ "BC",
505         /*0c */ "BChannelinformation",
506         /*0d */ "BProtocol",
507         /*0e */ "CalledPartyNumber",
508         /*0f */ "CalledPartySubaddress",
509         /*10 */ "CallingPartyNumber",
510         /*11 */ "CallingPartySubaddress",
511         /*12 */ "CIPmask",
512         /*13 */ "CIPmask2",
513         /*14 */ "CIPValue",
514         /*15 */ "Class",
515         /*16 */ "ConnectedNumber",
516         /*17 */ "ConnectedSubaddress",
517         /*18 */ "Data32",
518         /*19 */ "DataHandle",
519         /*1a */ "DataLength",
520         /*1b */ "FacilityConfirmationParameter",
521         /*1c */ "Facilitydataarray",
522         /*1d */ "FacilityIndicationParameter",
523         /*1e */ "FacilityRequestParameter",
524         /*1f */ "FacilitySelector",
525         /*20 */ "Flags",
526         /*21 */ "Function",
527         /*22 */ "HLC",
528         /*23 */ "Info",
529         /*24 */ "InfoElement",
530         /*25 */ "InfoMask",
531         /*26 */ "InfoNumber",
532         /*27 */ "Keypadfacility",
533         /*28 */ "LLC",
534         /*29 */ "ManuData",
535         /*2a */ "ManuID",
536         /*2b */ "NCPI",
537         /*2c */ "Reason",
538         /*2d */ "Reason_B3",
539         /*2e */ "Reject",
540         /*2f */ "Useruserdata"
541 };
542
543
544
545 #include <stdarg.h>
546
547 /*-------------------------------------------------------*/
548 static _cdebbuf *bufprint(_cdebbuf *cdb, char *fmt, ...)
549 {
550         va_list f;
551         size_t n, r;
552
553         if (!cdb)
554                 return NULL;
555         va_start(f, fmt);
556         r = cdb->size - cdb->pos;
557         n = vsnprintf(cdb->p, r, fmt, f);
558         va_end(f);
559         if (n >= r) {
560                 /* truncated, need bigger buffer */
561                 size_t ns = 2 * cdb->size;
562                 u_char *nb;
563
564                 while ((ns - cdb->pos) <= n)
565                         ns *= 2;
566                 nb = kmalloc(ns, GFP_ATOMIC);
567                 if (!nb) {
568                         cdebbuf_free(cdb);
569                         return NULL;
570                 }
571                 memcpy(nb, cdb->buf, cdb->pos);
572                 kfree(cdb->buf);
573                 nb[cdb->pos] = 0;
574                 cdb->buf = nb;
575                 cdb->p = cdb->buf + cdb->pos;
576                 cdb->size = ns;
577                 va_start(f, fmt);
578                 r = cdb->size - cdb->pos;
579                 n = vsnprintf(cdb->p, r, fmt, f);
580                 va_end(f);
581         }
582         cdb->p += n;
583         cdb->pos += n;
584         return cdb;
585 }
586
587 static _cdebbuf *printstructlen(_cdebbuf *cdb, u8 *m, unsigned len)
588 {
589         unsigned hex = 0;
590
591         if (!cdb)
592                 return NULL;
593         for (; len; len--, m++)
594                 if (isalnum(*m) || *m == ' ') {
595                         if (hex)
596                                 cdb = bufprint(cdb, ">");
597                         cdb = bufprint(cdb, "%c", *m);
598                         hex = 0;
599                 } else {
600                         if (!hex)
601                                 cdb = bufprint(cdb, "<%02x", *m);
602                         else
603                                 cdb = bufprint(cdb, " %02x", *m);
604                         hex = 1;
605                 }
606         if (hex)
607                 cdb = bufprint(cdb, ">");
608         return cdb;
609 }
610
611 static _cdebbuf *printstruct(_cdebbuf *cdb, u8 *m)
612 {
613         unsigned len;
614
615         if (m[0] != 0xff) {
616                 len = m[0];
617                 m += 1;
618         } else {
619                 len = ((u16 *) (m + 1))[0];
620                 m += 3;
621         }
622         cdb = printstructlen(cdb, m, len);
623         return cdb;
624 }
625
626 /*-------------------------------------------------------*/
627 #define NAME (pnames[cmsg->par[cmsg->p]])
628
629 static _cdebbuf *protocol_message_2_pars(_cdebbuf *cdb, _cmsg *cmsg, int level)
630 {
631         for (; TYP != _CEND; cmsg->p++) {
632                 int slen = 29 + 3 - level;
633                 int i;
634
635                 if (!cdb)
636                         return NULL;
637                 cdb = bufprint(cdb, "  ");
638                 for (i = 0; i < level - 1; i++)
639                         cdb = bufprint(cdb, " ");
640
641                 switch (TYP) {
642                 case _CBYTE:
643                         cdb = bufprint(cdb, "%-*s = 0x%x\n", slen, NAME, *(u8 *) (cmsg->m + cmsg->l));
644                         cmsg->l++;
645                         break;
646                 case _CWORD:
647                         cdb = bufprint(cdb, "%-*s = 0x%x\n", slen, NAME, *(u16 *) (cmsg->m + cmsg->l));
648                         cmsg->l += 2;
649                         break;
650                 case _CDWORD:
651                         cdb = bufprint(cdb, "%-*s = 0x%lx\n", slen, NAME, *(u32 *) (cmsg->m + cmsg->l));
652                         cmsg->l += 4;
653                         break;
654                 case _CSTRUCT:
655                         cdb = bufprint(cdb, "%-*s = ", slen, NAME);
656                         if (cmsg->m[cmsg->l] == '\0')
657                                 cdb = bufprint(cdb, "default");
658                         else
659                                 cdb = printstruct(cdb, cmsg->m + cmsg->l);
660                         cdb = bufprint(cdb, "\n");
661                         if (cmsg->m[cmsg->l] != 0xff)
662                                 cmsg->l += 1 + cmsg->m[cmsg->l];
663                         else
664                                 cmsg->l += 3 + *(u16 *) (cmsg->m + cmsg->l + 1);
665
666                         break;
667
668                 case _CMSTRUCT:
669 /*----- Metastruktur 0 -----*/
670                         if (cmsg->m[cmsg->l] == '\0') {
671                                 cdb = bufprint(cdb, "%-*s = default\n", slen, NAME);
672                                 cmsg->l++;
673                                 jumpcstruct(cmsg);
674                         } else {
675                                 char *name = NAME;
676                                 unsigned _l = cmsg->l;
677                                 cdb = bufprint(cdb, "%-*s\n", slen, name);
678                                 cmsg->l = (cmsg->m + _l)[0] == 255 ? cmsg->l + 3 : cmsg->l + 1;
679                                 cmsg->p++;
680                                 cdb = protocol_message_2_pars(cdb, cmsg, level + 1);
681                         }
682                         break;
683                 }
684         }
685         return cdb;
686 }
687 /*-------------------------------------------------------*/
688
689 static _cdebbuf *g_debbuf;
690 static u_long g_debbuf_lock;
691 static _cmsg *g_cmsg;
692
693 static _cdebbuf *cdebbuf_alloc(void)
694 {
695         _cdebbuf *cdb;
696
697         if (likely(!test_and_set_bit(1, &g_debbuf_lock))) {
698                 cdb = g_debbuf;
699                 goto init;
700         } else
701                 cdb = kmalloc(sizeof(_cdebbuf), GFP_ATOMIC);
702         if (!cdb)
703                 return NULL;
704         cdb->buf = kmalloc(CDEBUG_SIZE, GFP_ATOMIC);
705         if (!cdb->buf) {
706                 kfree(cdb);
707                 return NULL;
708         }
709         cdb->size = CDEBUG_SIZE;
710 init:
711         cdb->buf[0] = 0;
712         cdb->p = cdb->buf;
713         cdb->pos = 0;
714         return cdb;
715 }
716
717 /**
718  * cdebbuf_free() - free CAPI debug buffer
719  * @cdb:        buffer to free
720  */
721
722 void cdebbuf_free(_cdebbuf *cdb)
723 {
724         if (likely(cdb == g_debbuf)) {
725                 test_and_clear_bit(1, &g_debbuf_lock);
726                 return;
727         }
728         if (likely(cdb))
729                 kfree(cdb->buf);
730         kfree(cdb);
731 }
732
733
734 /**
735  * capi_message2str() - format CAPI 2.0 message for printing
736  * @msg:        CAPI 2.0 message
737  *
738  * Allocates a CAPI debug buffer and fills it with a printable representation
739  * of the CAPI 2.0 message in @msg.
740  * Return value: allocated debug buffer, NULL on error
741  * The returned buffer should be freed by a call to cdebbuf_free() after use.
742  */
743
744 _cdebbuf *capi_message2str(u8 *msg)
745 {
746         _cdebbuf *cdb;
747         _cmsg   *cmsg;
748
749         cdb = cdebbuf_alloc();
750         if (unlikely(!cdb))
751                 return NULL;
752         if (likely(cdb == g_debbuf))
753                 cmsg = g_cmsg;
754         else
755                 cmsg = kmalloc(sizeof(_cmsg), GFP_ATOMIC);
756         if (unlikely(!cmsg)) {
757                 cdebbuf_free(cdb);
758                 return NULL;
759         }
760         cmsg->m = msg;
761         cmsg->l = 8;
762         cmsg->p = 0;
763         byteTRcpy(cmsg->m + 4, &cmsg->Command);
764         byteTRcpy(cmsg->m + 5, &cmsg->Subcommand);
765         cmsg->par = cpars[command_2_index(cmsg->Command, cmsg->Subcommand)];
766
767         cdb = bufprint(cdb, "%-26s ID=%03d #0x%04x LEN=%04d\n",
768                        mnames[command_2_index(cmsg->Command, cmsg->Subcommand)],
769                        ((unsigned short *) msg)[1],
770                        ((unsigned short *) msg)[3],
771                        ((unsigned short *) msg)[0]);
772
773         cdb = protocol_message_2_pars(cdb, cmsg, 1);
774         if (unlikely(cmsg != g_cmsg))
775                 kfree(cmsg);
776         return cdb;
777 }
778
779 /**
780  * capi_cmsg2str() - format _cmsg structure for printing
781  * @cmsg:       _cmsg structure
782  *
783  * Allocates a CAPI debug buffer and fills it with a printable representation
784  * of the CAPI 2.0 message stored in @cmsg by a previous call to
785  * capi_cmsg2message() or capi_message2cmsg().
786  * Return value: allocated debug buffer, NULL on error
787  * The returned buffer should be freed by a call to cdebbuf_free() after use.
788  */
789
790 _cdebbuf *capi_cmsg2str(_cmsg *cmsg)
791 {
792         _cdebbuf *cdb;
793
794         if (!cmsg->m)
795                 return NULL;    /* no message */
796         cdb = cdebbuf_alloc();
797         if (!cdb)
798                 return NULL;
799         cmsg->l = 8;
800         cmsg->p = 0;
801         cdb = bufprint(cdb, "%s ID=%03d #0x%04x LEN=%04d\n",
802                        mnames[command_2_index(cmsg->Command, cmsg->Subcommand)],
803                        ((u16 *) cmsg->m)[1],
804                        ((u16 *) cmsg->m)[3],
805                        ((u16 *) cmsg->m)[0]);
806         cdb = protocol_message_2_pars(cdb, cmsg, 1);
807         return cdb;
808 }
809
810 int __init cdebug_init(void)
811 {
812         g_cmsg = kmalloc(sizeof(_cmsg), GFP_KERNEL);
813         if (!g_cmsg)
814                 return -ENOMEM;
815         g_debbuf = kmalloc(sizeof(_cdebbuf), GFP_KERNEL);
816         if (!g_debbuf) {
817                 kfree(g_cmsg);
818                 return -ENOMEM;
819         }
820         g_debbuf->buf = kmalloc(CDEBUG_GSIZE, GFP_KERNEL);
821         if (!g_debbuf->buf) {
822                 kfree(g_cmsg);
823                 kfree(g_debbuf);
824                 return -ENOMEM;
825         }
826         g_debbuf->size = CDEBUG_GSIZE;
827         g_debbuf->buf[0] = 0;
828         g_debbuf->p = g_debbuf->buf;
829         g_debbuf->pos = 0;
830         return 0;
831 }
832
833 void __exit cdebug_exit(void)
834 {
835         if (g_debbuf)
836                 kfree(g_debbuf->buf);
837         kfree(g_debbuf);
838         kfree(g_cmsg);
839 }
840
841 #else /* !CONFIG_CAPI_TRACE */
842
843 static _cdebbuf g_debbuf = {"CONFIG_CAPI_TRACE not enabled", NULL, 0, 0};
844
845 _cdebbuf *capi_message2str(u8 *msg)
846 {
847         return &g_debbuf;
848 }
849
850 _cdebbuf *capi_cmsg2str(_cmsg *cmsg)
851 {
852         return &g_debbuf;
853 }
854
855 void cdebbuf_free(_cdebbuf *cdb)
856 {
857 }
858
859 int __init cdebug_init(void)
860 {
861         return 0;
862 }
863
864 void __exit cdebug_exit(void)
865 {
866 }
867
868 #endif
869
870 EXPORT_SYMBOL(cdebbuf_free);
871 EXPORT_SYMBOL(capi_cmsg2message);
872 EXPORT_SYMBOL(capi_message2cmsg);
873 EXPORT_SYMBOL(capi_cmsg_header);
874 EXPORT_SYMBOL(capi_cmd2str);
875 EXPORT_SYMBOL(capi_cmsg2str);
876 EXPORT_SYMBOL(capi_message2str);