import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / drivers / isdn / avmb1 / capiutil.c
1 /* $Id: capiutil.c,v 1.1.1.1 2005/04/11 02:50:22 jack 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 <asm/segment.h>
21 #include <linux/config.h>
22 #include "capiutil.h"
23
24 MODULE_DESCRIPTION("CAPI4Linux: CAPI message conversion support");
25 MODULE_AUTHOR("Carsten Paeth");
26 MODULE_LICENSE("GPL");
27
28 /* from CAPI2.0 DDK AVM Berlin GmbH */
29
30 #ifndef CONFIG_ISDN_DRV_AVMB1_VERBOSE_REASON
31 char *capi_info2str(__u16 reason)
32 {
33     return "..";
34 }
35 #else
36 char *capi_info2str(__u16 reason)
37 {
38     switch (reason) {
39
40 /*-- informative values (corresponding message was processed) -----*/
41         case 0x0001:
42            return "NCPI not supported by current protocol, NCPI ignored";
43         case 0x0002:
44            return "Flags not supported by current protocol, flags ignored";
45         case 0x0003:
46            return "Alert already sent by another application";
47
48 /*-- error information concerning CAPI_REGISTER -----*/
49         case 0x1001:
50            return "Too many applications";
51         case 0x1002:
52            return "Logical block size too small, must be at least 128 Bytes";
53         case 0x1003:
54            return "Buffer exceeds 64 kByte";
55         case 0x1004:
56            return "Message buffer size too small, must be at least 1024 Bytes";
57         case 0x1005:
58            return "Max. number of logical connections not supported";
59         case 0x1006:
60            return "Reserved";
61         case 0x1007:
62            return "The message could not be accepted because of an internal busy condition";
63         case 0x1008:
64            return "OS resource error (no memory ?)";
65         case 0x1009:
66            return "CAPI not installed";
67         case 0x100A:
68            return "Controller does not support external equipment";
69         case 0x100B:
70            return "Controller does only support external equipment";
71
72 /*-- error information concerning message exchange functions -----*/
73         case 0x1101:
74            return "Illegal application number";
75         case 0x1102:
76            return "Illegal command or subcommand or message length less than 12 bytes";
77         case 0x1103:
78            return "The message could not be accepted because of a queue full condition !! The error code does not imply that CAPI cannot receive messages directed to another controller, PLCI or NCCI";
79         case 0x1104:
80            return "Queue is empty";
81         case 0x1105:
82            return "Queue overflow, a message was lost !! This indicates a configuration error. The only recovery from this error is to perform a CAPI_RELEASE";
83         case 0x1106:
84            return "Unknown notification parameter";
85         case 0x1107:
86            return "The Message could not be accepted because of an internal busy condition";
87         case 0x1108:
88            return "OS Resource error (no memory ?)";
89         case 0x1109:
90            return "CAPI not installed";
91         case 0x110A:
92            return "Controller does not support external equipment";
93         case 0x110B:
94            return "Controller does only support external equipment";
95
96 /*-- error information concerning resource / coding problems -----*/
97         case 0x2001:
98            return "Message not supported in current state";
99         case 0x2002:
100            return "Illegal Controller / PLCI / NCCI";
101         case 0x2003:
102            return "Out of PLCI";
103         case 0x2004:
104            return "Out of NCCI";
105         case 0x2005:
106            return "Out of LISTEN";
107         case 0x2006:
108            return "Out of FAX resources (protocol T.30)";
109         case 0x2007:
110            return "Illegal message parameter coding";
111
112 /*-- error information concerning requested services  -----*/
113         case 0x3001:
114            return "B1 protocol not supported";
115         case 0x3002: 
116            return "B2 protocol not supported";
117         case 0x3003: 
118            return "B3 protocol not supported";
119         case 0x3004: 
120            return "B1 protocol parameter not supported";
121         case 0x3005: 
122            return "B2 protocol parameter not supported";
123         case 0x3006: 
124            return "B3 protocol parameter not supported";
125         case 0x3007: 
126            return "B protocol combination not supported";
127         case 0x3008: 
128            return "NCPI not supported";
129         case 0x3009: 
130            return "CIP Value unknown";
131         case 0x300A: 
132            return "Flags not supported (reserved bits)";
133         case 0x300B: 
134            return "Facility not supported";
135         case 0x300C: 
136            return "Data length not supported by current protocol";
137         case 0x300D: 
138            return "Reset procedure not supported by current protocol";
139
140 /*-- informations about the clearing of a physical connection -----*/
141         case 0x3301: 
142            return "Protocol error layer 1 (broken line or B-channel removed by signalling protocol)";
143         case 0x3302: 
144            return "Protocol error layer 2";
145         case 0x3303: 
146            return "Protocol error layer 3";
147         case 0x3304: 
148            return "Another application got that call";
149 /*-- T.30 specific reasons -----*/
150         case 0x3311: 
151            return "Connecting not successful (remote station is no FAX G3 machine)";
152         case 0x3312: 
153            return "Connecting not successful (training error)";
154         case 0x3313: 
155            return "Disconnected before transfer (remote station does not support transfer mode, e.g. resolution)";
156         case 0x3314: 
157            return "Disconnected during transfer (remote abort)";
158         case 0x3315: 
159            return "Disconnected during transfer (remote procedure error, e.g. unsuccessful repetition of T.30 commands)";
160         case 0x3316: 
161            return "Disconnected during transfer (local tx data underrun)";
162         case 0x3317: 
163            return "Disconnected during transfer (local rx data overflow)";
164         case 0x3318: 
165            return "Disconnected during transfer (local abort)";
166         case 0x3319: 
167            return "Illegal parameter coding (e.g. SFF coding error)";
168
169 /*-- disconnect causes from the network according to ETS 300 102-1/Q.931 -----*/
170         case 0x3481: return "Unallocated (unassigned) number";
171         case 0x3482: return "No route to specified transit network";
172         case 0x3483: return "No route to destination";
173         case 0x3486: return "Channel unacceptable";
174         case 0x3487: 
175            return "Call awarded and being delivered in an established channel";
176         case 0x3490: return "Normal call clearing";
177         case 0x3491: return "User busy";
178         case 0x3492: return "No user responding";
179         case 0x3493: return "No answer from user (user alerted)";
180         case 0x3495: return "Call rejected";
181         case 0x3496: return "Number changed";
182         case 0x349A: return "Non-selected user clearing";
183         case 0x349B: return "Destination out of order";
184         case 0x349C: return "Invalid number format";
185         case 0x349D: return "Facility rejected";
186         case 0x349E: return "Response to STATUS ENQUIRY";
187         case 0x349F: return "Normal, unspecified";
188         case 0x34A2: return "No circuit / channel available";
189         case 0x34A6: return "Network out of order";
190         case 0x34A9: return "Temporary failure";
191         case 0x34AA: return "Switching equipment congestion";
192         case 0x34AB: return "Access information discarded";
193         case 0x34AC: return "Requested circuit / channel not available";
194         case 0x34AF: return "Resources unavailable, unspecified";
195         case 0x34B1: return "Quality of service unavailable";
196         case 0x34B2: return "Requested facility not subscribed";
197         case 0x34B9: return "Bearer capability not authorized";
198         case 0x34BA: return "Bearer capability not presently available";
199         case 0x34BF: return "Service or option not available, unspecified";
200         case 0x34C1: return "Bearer capability not implemented";
201         case 0x34C2: return "Channel type not implemented";
202         case 0x34C5: return "Requested facility not implemented";
203         case 0x34C6: return "Only restricted digital information bearer capability is available";
204         case 0x34CF: return "Service or option not implemented, unspecified";
205         case 0x34D1: return "Invalid call reference value";
206         case 0x34D2: return "Identified channel does not exist";
207         case 0x34D3: return "A suspended call exists, but this call identity does not";
208         case 0x34D4: return "Call identity in use";
209         case 0x34D5: return "No call suspended";
210         case 0x34D6: return "Call having the requested call identity has been cleared";
211         case 0x34D8: return "Incompatible destination";
212         case 0x34DB: return "Invalid transit network selection";
213         case 0x34DF: return "Invalid message, unspecified";
214         case 0x34E0: return "Mandatory information element is missing";
215         case 0x34E1: return "Message type non-existent or not implemented";
216         case 0x34E2: return "Message not compatible with call state or message type non-existent or not implemented";
217         case 0x34E3: return "Information element non-existent or not implemented";
218         case 0x34E4: return "Invalid information element contents";
219         case 0x34E5: return "Message not compatible with call state";
220         case 0x34E6: return "Recovery on timer expiry";
221         case 0x34EF: return "Protocol error, unspecified";
222         case 0x34FF: return "Interworking, unspecified";
223
224         default: return "No additional information";
225     }
226 }
227 #endif
228
229 typedef struct {
230         int typ;
231         size_t off;
232 } _cdef;
233
234 #define _CBYTE         1
235 #define _CWORD         2
236 #define _CDWORD        3
237 #define _CSTRUCT       4
238 #define _CMSTRUCT      5
239 #define _CEND          6
240
241 static _cdef cdef[] =
242 {
243     /*00 */ 
244  {_CEND},
245     /*01 */ 
246  {_CEND},
247     /*02 */ 
248  {_CEND},
249     /*03 */ 
250  {_CDWORD, offsetof(_cmsg, adr.adrController)},
251     /*04 */ 
252  {_CMSTRUCT, offsetof(_cmsg, AdditionalInfo)},
253     /*05 */ 
254  {_CSTRUCT, offsetof(_cmsg, B1configuration)},
255     /*06 */ 
256  {_CWORD, offsetof(_cmsg, B1protocol)},
257     /*07 */ 
258  {_CSTRUCT, offsetof(_cmsg, B2configuration)},
259     /*08 */ 
260  {_CWORD, offsetof(_cmsg, B2protocol)},
261     /*09 */ 
262  {_CSTRUCT, offsetof(_cmsg, B3configuration)},
263     /*0a */ 
264  {_CWORD, offsetof(_cmsg, B3protocol)},
265     /*0b */ 
266  {_CSTRUCT, offsetof(_cmsg, BC)},
267     /*0c */ 
268  {_CSTRUCT, offsetof(_cmsg, BChannelinformation)},
269     /*0d */ 
270  {_CMSTRUCT, offsetof(_cmsg, BProtocol)},
271     /*0e */ 
272  {_CSTRUCT, offsetof(_cmsg, CalledPartyNumber)},
273     /*0f */ 
274  {_CSTRUCT, offsetof(_cmsg, CalledPartySubaddress)},
275     /*10 */ 
276  {_CSTRUCT, offsetof(_cmsg, CallingPartyNumber)},
277     /*11 */ 
278  {_CSTRUCT, offsetof(_cmsg, CallingPartySubaddress)},
279     /*12 */ 
280  {_CDWORD, offsetof(_cmsg, CIPmask)},
281     /*13 */ 
282  {_CDWORD, offsetof(_cmsg, CIPmask2)},
283     /*14 */ 
284  {_CWORD, offsetof(_cmsg, CIPValue)},
285     /*15 */ 
286  {_CDWORD, offsetof(_cmsg, Class)},
287     /*16 */ 
288  {_CSTRUCT, offsetof(_cmsg, ConnectedNumber)},
289     /*17 */ 
290  {_CSTRUCT, offsetof(_cmsg, ConnectedSubaddress)},
291     /*18 */ 
292  {_CDWORD, offsetof(_cmsg, Data)},
293     /*19 */ 
294  {_CWORD, offsetof(_cmsg, DataHandle)},
295     /*1a */ 
296  {_CWORD, offsetof(_cmsg, DataLength)},
297     /*1b */ 
298  {_CSTRUCT, offsetof(_cmsg, FacilityConfirmationParameter)},
299     /*1c */ 
300  {_CSTRUCT, offsetof(_cmsg, Facilitydataarray)},
301     /*1d */ 
302  {_CSTRUCT, offsetof(_cmsg, FacilityIndicationParameter)},
303     /*1e */ 
304  {_CSTRUCT, offsetof(_cmsg, FacilityRequestParameter)},
305     /*1f */ 
306  {_CWORD, offsetof(_cmsg, FacilitySelector)},
307     /*20 */ 
308  {_CWORD, offsetof(_cmsg, Flags)},
309     /*21 */ 
310  {_CDWORD, offsetof(_cmsg, Function)},
311     /*22 */ 
312  {_CSTRUCT, offsetof(_cmsg, HLC)},
313     /*23 */ 
314  {_CWORD, offsetof(_cmsg, Info)},
315     /*24 */ 
316  {_CSTRUCT, offsetof(_cmsg, InfoElement)},
317     /*25 */ 
318  {_CDWORD, offsetof(_cmsg, InfoMask)},
319     /*26 */ 
320  {_CWORD, offsetof(_cmsg, InfoNumber)},
321     /*27 */ 
322  {_CSTRUCT, offsetof(_cmsg, Keypadfacility)},
323     /*28 */ 
324  {_CSTRUCT, offsetof(_cmsg, LLC)},
325     /*29 */ 
326  {_CSTRUCT, offsetof(_cmsg, ManuData)},
327     /*2a */ 
328  {_CDWORD, offsetof(_cmsg, ManuID)},
329     /*2b */ 
330  {_CSTRUCT, offsetof(_cmsg, NCPI)},
331     /*2c */ 
332  {_CWORD, offsetof(_cmsg, Reason)},
333     /*2d */ 
334  {_CWORD, offsetof(_cmsg, Reason_B3)},
335     /*2e */ 
336  {_CWORD, offsetof(_cmsg, Reject)},
337     /*2f */ 
338  {_CSTRUCT, offsetof(_cmsg, Useruserdata)}
339 };
340
341 static unsigned char *cpars[] =
342 {
343     /*00 */ 0,
344     /*01 ALERT_REQ */ (unsigned char *) "\x03\x04\x0c\x27\x2f\x1c\x01\x01",
345     /*02 CONNECT_REQ */ (unsigned char *) "\x03\x14\x0e\x10\x0f\x11\x0d\x06\x08\x0a\x05\x07\x09\x01\x0b\x28\x22\x04\x0c\x27\x2f\x1c\x01\x01",
346     /*03 */ 0,
347     /*04 DISCONNECT_REQ */ (unsigned char *) "\x03\x04\x0c\x27\x2f\x1c\x01\x01",
348     /*05 LISTEN_REQ */ (unsigned char *) "\x03\x25\x12\x13\x10\x11\x01",
349     /*06 */ 0,
350     /*07 */ 0,
351     /*08 INFO_REQ */ (unsigned char *) "\x03\x0e\x04\x0c\x27\x2f\x1c\x01\x01",
352     /*09 FACILITY_REQ */ (unsigned char *) "\x03\x1f\x1e\x01",
353     /*0a SELECT_B_PROTOCOL_REQ */ (unsigned char *) "\x03\x0d\x06\x08\x0a\x05\x07\x09\x01\x01",
354     /*0b CONNECT_B3_REQ */ (unsigned char *) "\x03\x2b\x01",
355     /*0c */ 0,
356     /*0d DISCONNECT_B3_REQ */ (unsigned char *) "\x03\x2b\x01",
357     /*0e */ 0,
358     /*0f DATA_B3_REQ */ (unsigned char *) "\x03\x18\x1a\x19\x20\x01",
359     /*10 RESET_B3_REQ */ (unsigned char *) "\x03\x2b\x01",
360     /*11 */ 0,
361     /*12 */ 0,
362     /*13 ALERT_CONF */ (unsigned char *) "\x03\x23\x01",
363     /*14 CONNECT_CONF */ (unsigned char *) "\x03\x23\x01",
364     /*15 */ 0,
365     /*16 DISCONNECT_CONF */ (unsigned char *) "\x03\x23\x01",
366     /*17 LISTEN_CONF */ (unsigned char *) "\x03\x23\x01",
367     /*18 MANUFACTURER_REQ */ (unsigned char *) "\x03\x2a\x15\x21\x29\x01",
368     /*19 */ 0,
369     /*1a INFO_CONF */ (unsigned char *) "\x03\x23\x01",
370     /*1b FACILITY_CONF */ (unsigned char *) "\x03\x23\x1f\x1b\x01",
371     /*1c SELECT_B_PROTOCOL_CONF */ (unsigned char *) "\x03\x23\x01",
372     /*1d CONNECT_B3_CONF */ (unsigned char *) "\x03\x23\x01",
373     /*1e */ 0,
374     /*1f DISCONNECT_B3_CONF */ (unsigned char *) "\x03\x23\x01",
375     /*20 */ 0,
376     /*21 DATA_B3_CONF */ (unsigned char *) "\x03\x19\x23\x01",
377     /*22 RESET_B3_CONF */ (unsigned char *) "\x03\x23\x01",
378     /*23 */ 0,
379     /*24 */ 0,
380     /*25 */ 0,
381     /*26 CONNECT_IND */ (unsigned char *) "\x03\x14\x0e\x10\x0f\x11\x0b\x28\x22\x04\x0c\x27\x2f\x1c\x01\x01",
382     /*27 CONNECT_ACTIVE_IND */ (unsigned char *) "\x03\x16\x17\x28\x01",
383     /*28 DISCONNECT_IND */ (unsigned char *) "\x03\x2c\x01",
384     /*29 */ 0,
385     /*2a MANUFACTURER_CONF */ (unsigned char *) "\x03\x2a\x15\x21\x29\x01",
386     /*2b */ 0,
387     /*2c INFO_IND */ (unsigned char *) "\x03\x26\x24\x01",
388     /*2d FACILITY_IND */ (unsigned char *) "\x03\x1f\x1d\x01",
389     /*2e */ 0,
390     /*2f CONNECT_B3_IND */ (unsigned char *) "\x03\x2b\x01",
391     /*30 CONNECT_B3_ACTIVE_IND */ (unsigned char *) "\x03\x2b\x01",
392     /*31 DISCONNECT_B3_IND */ (unsigned char *) "\x03\x2d\x2b\x01",
393     /*32 */ 0,
394     /*33 DATA_B3_IND */ (unsigned char *) "\x03\x18\x1a\x19\x20\x01",
395     /*34 RESET_B3_IND */ (unsigned char *) "\x03\x2b\x01",
396     /*35 CONNECT_B3_T90_ACTIVE_IND */ (unsigned char *) "\x03\x2b\x01",
397     /*36 */ 0,
398     /*37 */ 0,
399     /*38 CONNECT_RESP */ (unsigned char *) "\x03\x2e\x0d\x06\x08\x0a\x05\x07\x09\x01\x16\x17\x28\x04\x0c\x27\x2f\x1c\x01\x01",
400     /*39 CONNECT_ACTIVE_RESP */ (unsigned char *) "\x03\x01",
401     /*3a DISCONNECT_RESP */ (unsigned char *) "\x03\x01",
402     /*3b */ 0,
403     /*3c MANUFACTURER_IND */ (unsigned char *) "\x03\x2a\x15\x21\x29\x01",
404     /*3d */ 0,
405     /*3e INFO_RESP */ (unsigned char *) "\x03\x01",
406     /*3f FACILITY_RESP */ (unsigned char *) "\x03\x1f\x01",
407     /*40 */ 0,
408     /*41 CONNECT_B3_RESP */ (unsigned char *) "\x03\x2e\x2b\x01",
409     /*42 CONNECT_B3_ACTIVE_RESP */ (unsigned char *) "\x03\x01",
410     /*43 DISCONNECT_B3_RESP */ (unsigned char *) "\x03\x01",
411     /*44 */ 0,
412     /*45 DATA_B3_RESP */ (unsigned char *) "\x03\x19\x01",
413     /*46 RESET_B3_RESP */ (unsigned char *) "\x03\x01",
414     /*47 CONNECT_B3_T90_ACTIVE_RESP */ (unsigned char *) "\x03\x01",
415     /*48 */ 0,
416     /*49 */ 0,
417     /*4a */ 0,
418     /*4b */ 0,
419     /*4c */ 0,
420     /*4d */ 0,
421     /*4e MANUFACTURER_RESP */ (unsigned char *) "\x03\x2a\x15\x21\x29\x01",
422 };
423
424 /*-------------------------------------------------------*/
425
426 #define byteTLcpy(x,y)        *(__u8 *)(x)=*(__u8 *)(y);
427 #define wordTLcpy(x,y)        *(__u16 *)(x)=*(__u16 *)(y);
428 #define dwordTLcpy(x,y)       memcpy(x,y,4);
429 #define structTLcpy(x,y,l)    memcpy (x,y,l)
430 #define structTLcpyovl(x,y,l) memmove (x,y,l)
431
432 #define byteTRcpy(x,y)        *(__u8 *)(y)=*(__u8 *)(x);
433 #define wordTRcpy(x,y)        *(__u16 *)(y)=*(__u16 *)(x);
434 #define dwordTRcpy(x,y)       memcpy(y,x,4);
435 #define structTRcpy(x,y,l)    memcpy (y,x,l)
436 #define structTRcpyovl(x,y,l) memmove (y,x,l)
437
438 /*-------------------------------------------------------*/
439 static unsigned command_2_index(unsigned c, unsigned sc)
440 {
441         if (c & 0x80)
442                 c = 0x9 + (c & 0x0f);
443         else if (c <= 0x0f);
444         else if (c == 0x41)
445                 c = 0x9 + 0x1;
446         else if (c == 0xff)
447                 c = 0x00;
448         return (sc & 3) * (0x9 + 0x9) + c;
449 }
450
451 /*-------------------------------------------------------*/
452 #define TYP (cdef[cmsg->par[cmsg->p]].typ)
453 #define OFF (((__u8 *)cmsg)+cdef[cmsg->par[cmsg->p]].off)
454
455 static void jumpcstruct(_cmsg * cmsg)
456 {
457         unsigned layer;
458         for (cmsg->p++, layer = 1; layer;) {
459                 /* $$$$$ assert (cmsg->p); */
460                 cmsg->p++;
461                 switch (TYP) {
462                 case _CMSTRUCT:
463                         layer++;
464                         break;
465                 case _CEND:
466                         layer--;
467                         break;
468                 }
469         }
470 }
471 /*-------------------------------------------------------*/
472 static void pars_2_message(_cmsg * cmsg)
473 {
474
475         for (; TYP != _CEND; cmsg->p++) {
476                 switch (TYP) {
477                 case _CBYTE:
478                         byteTLcpy(cmsg->m + cmsg->l, OFF);
479                         cmsg->l++;
480                         break;
481                 case _CWORD:
482                         wordTLcpy(cmsg->m + cmsg->l, OFF);
483                         cmsg->l += 2;
484                         break;
485                 case _CDWORD:
486                         dwordTLcpy(cmsg->m + cmsg->l, OFF);
487                         cmsg->l += 4;
488                         break;
489                 case _CSTRUCT:
490                         if (*(__u8 **) OFF == 0) {
491                                 *(cmsg->m + cmsg->l) = '\0';
492                                 cmsg->l++;
493                         } else if (**(_cstruct *) OFF != 0xff) {
494                                 structTLcpy(cmsg->m + cmsg->l, *(_cstruct *) OFF, 1 + **(_cstruct *) OFF);
495                                 cmsg->l += 1 + **(_cstruct *) OFF;
496                         } else {
497                                 _cstruct s = *(_cstruct *) OFF;
498                                 structTLcpy(cmsg->m + cmsg->l, s, 3 + *(__u16 *) (s + 1));
499                                 cmsg->l += 3 + *(__u16 *) (s + 1);
500                         }
501                         break;
502                 case _CMSTRUCT:
503 /*----- Metastruktur 0 -----*/
504                         if (*(_cmstruct *) OFF == CAPI_DEFAULT) {
505                                 *(cmsg->m + cmsg->l) = '\0';
506                                 cmsg->l++;
507                                 jumpcstruct(cmsg);
508                         }
509 /*----- Metastruktur wird composed -----*/
510                         else {
511                                 unsigned _l = cmsg->l;
512                                 unsigned _ls;
513                                 cmsg->l++;
514                                 cmsg->p++;
515                                 pars_2_message(cmsg);
516                                 _ls = cmsg->l - _l - 1;
517                                 if (_ls < 255)
518                                         (cmsg->m + _l)[0] = (__u8) _ls;
519                                 else {
520                                         structTLcpyovl(cmsg->m + _l + 3, cmsg->m + _l + 1, _ls);
521                                         (cmsg->m + _l)[0] = 0xff;
522                                         wordTLcpy(cmsg->m + _l + 1, &_ls);
523                                 }
524                         }
525                         break;
526                 }
527         }
528 }
529
530 /*-------------------------------------------------------*/
531 unsigned capi_cmsg2message(_cmsg * cmsg, __u8 * msg)
532 {
533         cmsg->m = msg;
534         cmsg->l = 8;
535         cmsg->p = 0;
536         cmsg->par = cpars[command_2_index(cmsg->Command, cmsg->Subcommand)];
537
538         pars_2_message(cmsg);
539
540         wordTLcpy(msg + 0, &cmsg->l);
541         byteTLcpy(cmsg->m + 4, &cmsg->Command);
542         byteTLcpy(cmsg->m + 5, &cmsg->Subcommand);
543         wordTLcpy(cmsg->m + 2, &cmsg->ApplId);
544         wordTLcpy(cmsg->m + 6, &cmsg->Messagenumber);
545
546         return 0;
547 }
548
549 /*-------------------------------------------------------*/
550 static void message_2_pars(_cmsg * cmsg)
551 {
552         for (; TYP != _CEND; cmsg->p++) {
553
554                 switch (TYP) {
555                 case _CBYTE:
556                         byteTRcpy(cmsg->m + cmsg->l, OFF);
557                         cmsg->l++;
558                         break;
559                 case _CWORD:
560                         wordTRcpy(cmsg->m + cmsg->l, OFF);
561                         cmsg->l += 2;
562                         break;
563                 case _CDWORD:
564                         dwordTRcpy(cmsg->m + cmsg->l, OFF);
565                         cmsg->l += 4;
566                         break;
567                 case _CSTRUCT:
568                         *(__u8 **) OFF = cmsg->m + cmsg->l;
569
570                         if (cmsg->m[cmsg->l] != 0xff)
571                                 cmsg->l += 1 + cmsg->m[cmsg->l];
572                         else
573                                 cmsg->l += 3 + *(__u16 *) (cmsg->m + cmsg->l + 1);
574                         break;
575                 case _CMSTRUCT:
576 /*----- Metastruktur 0 -----*/
577                         if (cmsg->m[cmsg->l] == '\0') {
578                                 *(_cmstruct *) OFF = CAPI_DEFAULT;
579                                 cmsg->l++;
580                                 jumpcstruct(cmsg);
581                         } else {
582                                 unsigned _l = cmsg->l;
583                                 *(_cmstruct *) OFF = CAPI_COMPOSE;
584                                 cmsg->l = (cmsg->m + _l)[0] == 255 ? cmsg->l + 3 : cmsg->l + 1;
585                                 cmsg->p++;
586                                 message_2_pars(cmsg);
587                         }
588                         break;
589                 }
590         }
591 }
592
593 /*-------------------------------------------------------*/
594 unsigned capi_message2cmsg(_cmsg * cmsg, __u8 * msg)
595 {
596         memset(cmsg, 0, sizeof(_cmsg));
597         cmsg->m = msg;
598         cmsg->l = 8;
599         cmsg->p = 0;
600         byteTRcpy(cmsg->m + 4, &cmsg->Command);
601         byteTRcpy(cmsg->m + 5, &cmsg->Subcommand);
602         cmsg->par = cpars[command_2_index(cmsg->Command, cmsg->Subcommand)];
603
604         message_2_pars(cmsg);
605
606         wordTRcpy(msg + 0, &cmsg->l);
607         wordTRcpy(cmsg->m + 2, &cmsg->ApplId);
608         wordTRcpy(cmsg->m + 6, &cmsg->Messagenumber);
609
610         return 0;
611 }
612
613 /*-------------------------------------------------------*/
614 unsigned capi_cmsg_header(_cmsg * cmsg, __u16 _ApplId,
615                           __u8 _Command, __u8 _Subcommand,
616                           __u16 _Messagenumber, __u32 _Controller)
617 {
618         memset(cmsg, 0, sizeof(_cmsg));
619         cmsg->ApplId = _ApplId;
620         cmsg->Command = _Command;
621         cmsg->Subcommand = _Subcommand;
622         cmsg->Messagenumber = _Messagenumber;
623         cmsg->adr.adrController = _Controller;
624         return 0;
625 }
626
627 /*-------------------------------------------------------*/
628
629 static char *mnames[] =
630 {
631         0,
632         "ALERT_REQ",
633         "CONNECT_REQ",
634         0,
635         "DISCONNECT_REQ",
636         "LISTEN_REQ",
637         0,
638         0,
639         "INFO_REQ",
640         "FACILITY_REQ",
641         "SELECT_B_PROTOCOL_REQ",
642         "CONNECT_B3_REQ",
643         0,
644         "DISCONNECT_B3_REQ",
645         0,
646         "DATA_B3_REQ",
647         "RESET_B3_REQ",
648         0,
649         0,
650         "ALERT_CONF",
651         "CONNECT_CONF",
652         0,
653         "DISCONNECT_CONF",
654         "LISTEN_CONF",
655         "MANUFACTURER_REQ",
656         0,
657         "INFO_CONF",
658         "FACILITY_CONF",
659         "SELECT_B_PROTOCOL_CONF",
660         "CONNECT_B3_CONF",
661         0,
662         "DISCONNECT_B3_CONF",
663         0,
664         "DATA_B3_CONF",
665         "RESET_B3_CONF",
666         0,
667         0,
668         0,
669         "CONNECT_IND",
670         "CONNECT_ACTIVE_IND",
671         "DISCONNECT_IND",
672         0,
673         "MANUFACTURER_CONF",
674         0,
675         "INFO_IND",
676         "FACILITY_IND",
677         0,
678         "CONNECT_B3_IND",
679         "CONNECT_B3_ACTIVE_IND",
680         "DISCONNECT_B3_IND",
681         0,
682         "DATA_B3_IND",
683         "RESET_B3_IND",
684         "CONNECT_B3_T90_ACTIVE_IND",
685         0,
686         0,
687         "CONNECT_RESP",
688         "CONNECT_ACTIVE_RESP",
689         "DISCONNECT_RESP",
690         0,
691         "MANUFACTURER_IND",
692         0,
693         "INFO_RESP",
694         "FACILITY_RESP",
695         0,
696         "CONNECT_B3_RESP",
697         "CONNECT_B3_ACTIVE_RESP",
698         "DISCONNECT_B3_RESP",
699         0,
700         "DATA_B3_RESP",
701         "RESET_B3_RESP",
702         "CONNECT_B3_T90_ACTIVE_RESP",
703         0,
704         0,
705         0,
706         0,
707         0,
708         0,
709         "MANUFACTURER_RESP"
710 };
711
712 char *capi_cmd2str(__u8 cmd, __u8 subcmd)
713 {
714         return mnames[command_2_index(cmd, subcmd)];
715 }
716
717
718 /*-------------------------------------------------------*/
719 /*-------------------------------------------------------*/
720
721 static char *pnames[] =
722 {
723     /*00 */ 0,
724     /*01 */ 0,
725     /*02 */ 0,
726     /*03 */ "Controller/PLCI/NCCI",
727     /*04 */ "AdditionalInfo",
728     /*05 */ "B1configuration",
729     /*06 */ "B1protocol",
730     /*07 */ "B2configuration",
731     /*08 */ "B2protocol",
732     /*09 */ "B3configuration",
733     /*0a */ "B3protocol",
734     /*0b */ "BC",
735     /*0c */ "BChannelinformation",
736     /*0d */ "BProtocol",
737     /*0e */ "CalledPartyNumber",
738     /*0f */ "CalledPartySubaddress",
739     /*10 */ "CallingPartyNumber",
740     /*11 */ "CallingPartySubaddress",
741     /*12 */ "CIPmask",
742     /*13 */ "CIPmask2",
743     /*14 */ "CIPValue",
744     /*15 */ "Class",
745     /*16 */ "ConnectedNumber",
746     /*17 */ "ConnectedSubaddress",
747     /*18 */ "Data32",
748     /*19 */ "DataHandle",
749     /*1a */ "DataLength",
750     /*1b */ "FacilityConfirmationParameter",
751     /*1c */ "Facilitydataarray",
752     /*1d */ "FacilityIndicationParameter",
753     /*1e */ "FacilityRequestParameter",
754     /*1f */ "FacilitySelector",
755     /*20 */ "Flags",
756     /*21 */ "Function",
757     /*22 */ "HLC",
758     /*23 */ "Info",
759     /*24 */ "InfoElement",
760     /*25 */ "InfoMask",
761     /*26 */ "InfoNumber",
762     /*27 */ "Keypadfacility",
763     /*28 */ "LLC",
764     /*29 */ "ManuData",
765     /*2a */ "ManuID",
766     /*2b */ "NCPI",
767     /*2c */ "Reason",
768     /*2d */ "Reason_B3",
769     /*2e */ "Reject",
770     /*2f */ "Useruserdata"
771 };
772
773
774 static char buf[8192];
775 static char *p = 0;
776
777 #include <stdarg.h>
778
779 /*-------------------------------------------------------*/
780 static void bufprint(char *fmt,...)
781 {
782         va_list f;
783         va_start(f, fmt);
784         vsprintf(p, fmt, f);
785         va_end(f);
786         p += strlen(p);
787 }
788
789 static void printstructlen(__u8 * m, unsigned len)
790 {
791         unsigned hex = 0;
792         for (; len; len--, m++)
793                 if (isalnum(*m) || *m == ' ') {
794                         if (hex)
795                                 bufprint(">");
796                         bufprint("%c", *m);
797                         hex = 0;
798                 } else {
799                         if (!hex)
800                                 bufprint("<%02x", *m);
801                         else
802                                 bufprint(" %02x", *m);
803                         hex = 1;
804                 }
805         if (hex)
806                 bufprint(">");
807 }
808
809 static void printstruct(__u8 * m)
810 {
811         unsigned len;
812         if (m[0] != 0xff) {
813                 len = m[0];
814                 m += 1;
815         } else {
816                 len = ((__u16 *) (m + 1))[0];
817                 m += 3;
818         }
819         printstructlen(m, len);
820 }
821
822 /*-------------------------------------------------------*/
823 #define NAME (pnames[cmsg->par[cmsg->p]])
824
825 static void protocol_message_2_pars(_cmsg * cmsg, int level)
826 {
827         for (; TYP != _CEND; cmsg->p++) {
828                 int slen = 29 + 3 - level;
829                 int i;
830
831                 bufprint("  ");
832                 for (i = 0; i < level - 1; i++)
833                         bufprint(" ");
834
835                 switch (TYP) {
836                 case _CBYTE:
837                         bufprint("%-*s = 0x%x\n", slen, NAME, *(__u8 *) (cmsg->m + cmsg->l));
838                         cmsg->l++;
839                         break;
840                 case _CWORD:
841                         bufprint("%-*s = 0x%x\n", slen, NAME, *(__u16 *) (cmsg->m + cmsg->l));
842                         cmsg->l += 2;
843                         break;
844                 case _CDWORD:
845                         bufprint("%-*s = 0x%lx\n", slen, NAME, *(__u32 *) (cmsg->m + cmsg->l));
846                         cmsg->l += 4;
847                         break;
848                 case _CSTRUCT:
849                         bufprint("%-*s = ", slen, NAME);
850                         if (cmsg->m[cmsg->l] == '\0')
851                                 bufprint("default");
852                         else
853                                 printstruct(cmsg->m + cmsg->l);
854                         bufprint("\n");
855                         if (cmsg->m[cmsg->l] != 0xff)
856                                 cmsg->l += 1 + cmsg->m[cmsg->l];
857                         else
858                                 cmsg->l += 3 + *(__u16 *) (cmsg->m + cmsg->l + 1);
859
860                         break;
861
862                 case _CMSTRUCT:
863 /*----- Metastruktur 0 -----*/
864                         if (cmsg->m[cmsg->l] == '\0') {
865                                 bufprint("%-*s = default\n", slen, NAME);
866                                 cmsg->l++;
867                                 jumpcstruct(cmsg);
868                         } else {
869                                 char *name = NAME;
870                                 unsigned _l = cmsg->l;
871                                 bufprint("%-*s\n", slen, name);
872                                 cmsg->l = (cmsg->m + _l)[0] == 255 ? cmsg->l + 3 : cmsg->l + 1;
873                                 cmsg->p++;
874                                 protocol_message_2_pars(cmsg, level + 1);
875                         }
876                         break;
877                 }
878         }
879 }
880 /*-------------------------------------------------------*/
881 char *capi_message2str(__u8 * msg)
882 {
883
884         _cmsg cmsg;
885         p = buf;
886         p[0] = 0;
887
888         cmsg.m = msg;
889         cmsg.l = 8;
890         cmsg.p = 0;
891         byteTRcpy(cmsg.m + 4, &cmsg.Command);
892         byteTRcpy(cmsg.m + 5, &cmsg.Subcommand);
893         cmsg.par = cpars[command_2_index(cmsg.Command, cmsg.Subcommand)];
894
895         bufprint("%-26s ID=%03d #0x%04x LEN=%04d\n",
896                  mnames[command_2_index(cmsg.Command, cmsg.Subcommand)],
897                  ((unsigned short *) msg)[1],
898                  ((unsigned short *) msg)[3],
899                  ((unsigned short *) msg)[0]);
900
901         protocol_message_2_pars(&cmsg, 1);
902         return buf;
903 }
904
905 char *capi_cmsg2str(_cmsg * cmsg)
906 {
907         p = buf;
908         p[0] = 0;
909         cmsg->l = 8;
910         cmsg->p = 0;
911         bufprint("%s ID=%03d #0x%04x LEN=%04d\n",
912                  mnames[command_2_index(cmsg->Command, cmsg->Subcommand)],
913                  ((__u16 *) cmsg->m)[1],
914                  ((__u16 *) cmsg->m)[3],
915                  ((__u16 *) cmsg->m)[0]);
916         protocol_message_2_pars(cmsg, 1);
917         return buf;
918 }
919
920 EXPORT_SYMBOL(capi_cmsg2message);
921 EXPORT_SYMBOL(capi_message2cmsg);
922 EXPORT_SYMBOL(capi_cmsg_header);
923 EXPORT_SYMBOL(capi_cmd2str);
924 EXPORT_SYMBOL(capi_cmsg2str);
925 EXPORT_SYMBOL(capi_message2str);
926 EXPORT_SYMBOL(capi_info2str);
927
928 static int __init capiutil_init(void)
929
930         return 0; 
931 }
932
933 static void __exit capiutil_exit(void)
934 {
935 }
936
937 module_init(capiutil_init);
938 module_exit(capiutil_exit);