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