File: /usr/src/linux/drivers/isdn/avmb1/capiutil.c

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