blob: d26f17033b6863af4550498df3812dca718326c1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: capiutil.c,v 1.13.6.4 2001/09/23 22:24:33 kai Exp $
2 *
3 * CAPI 2.0 convert capi message to capi message struct
4 *
5 * From CAPI 2.0 Development Kit AVM 1995 (msg.c)
6 * Rewritten for Linux 1996 by Carsten Paeth <calle@calle.de>
7 *
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
10 *
11 */
12
13#include <linux/module.h>
14#include <linux/string.h>
15#include <linux/ctype.h>
16#include <linux/stddef.h>
17#include <linux/kernel.h>
18#include <linux/mm.h>
19#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/isdn/capiutil.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23/* from CAPI2.0 DDK AVM Berlin GmbH */
24
25#ifndef CONFIG_ISDN_DRV_AVMB1_VERBOSE_REASON
26char *capi_info2str(u16 reason)
27{
Joe Perches475be4d2012-02-19 19:52:38 -080028 return "..";
Linus Torvalds1da177e2005-04-16 15:20:36 -070029}
30#else
31char *capi_info2str(u16 reason)
32{
Joe Perches475be4d2012-02-19 19:52:38 -080033 switch (reason) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/*-- informative values (corresponding message was processed) -----*/
36 case 0x0001:
Joe Perches475be4d2012-02-19 19:52:38 -080037 return "NCPI not supported by current protocol, NCPI ignored";
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 case 0x0002:
Joe Perches475be4d2012-02-19 19:52:38 -080039 return "Flags not supported by current protocol, flags ignored";
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 case 0x0003:
Joe Perches475be4d2012-02-19 19:52:38 -080041 return "Alert already sent by another application";
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*-- error information concerning CAPI_REGISTER -----*/
44 case 0x1001:
Joe Perches475be4d2012-02-19 19:52:38 -080045 return "Too many applications";
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 case 0x1002:
Joe Perches475be4d2012-02-19 19:52:38 -080047 return "Logical block size too small, must be at least 128 Bytes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 case 0x1003:
Joe Perches475be4d2012-02-19 19:52:38 -080049 return "Buffer exceeds 64 kByte";
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 case 0x1004:
Joe Perches475be4d2012-02-19 19:52:38 -080051 return "Message buffer size too small, must be at least 1024 Bytes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 case 0x1005:
Joe Perches475be4d2012-02-19 19:52:38 -080053 return "Max. number of logical connections not supported";
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 case 0x1006:
Joe Perches475be4d2012-02-19 19:52:38 -080055 return "Reserved";
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 case 0x1007:
Joe Perches475be4d2012-02-19 19:52:38 -080057 return "The message could not be accepted because of an internal busy condition";
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 case 0x1008:
Joe Perches475be4d2012-02-19 19:52:38 -080059 return "OS resource error (no memory ?)";
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 case 0x1009:
Joe Perches475be4d2012-02-19 19:52:38 -080061 return "CAPI not installed";
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 case 0x100A:
Joe Perches475be4d2012-02-19 19:52:38 -080063 return "Controller does not support external equipment";
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 case 0x100B:
Joe Perches475be4d2012-02-19 19:52:38 -080065 return "Controller does only support external equipment";
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/*-- error information concerning message exchange functions -----*/
68 case 0x1101:
Joe Perches475be4d2012-02-19 19:52:38 -080069 return "Illegal application number";
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 case 0x1102:
Joe Perches475be4d2012-02-19 19:52:38 -080071 return "Illegal command or subcommand or message length less than 12 bytes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 case 0x1103:
Joe Perches475be4d2012-02-19 19:52:38 -080073 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";
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 case 0x1104:
Joe Perches475be4d2012-02-19 19:52:38 -080075 return "Queue is empty";
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 case 0x1105:
Joe Perches475be4d2012-02-19 19:52:38 -080077 return "Queue overflow, a message was lost !! This indicates a configuration error. The only recovery from this error is to perform a CAPI_RELEASE";
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 case 0x1106:
Joe Perches475be4d2012-02-19 19:52:38 -080079 return "Unknown notification parameter";
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 case 0x1107:
Joe Perches475be4d2012-02-19 19:52:38 -080081 return "The Message could not be accepted because of an internal busy condition";
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 case 0x1108:
Joe Perches475be4d2012-02-19 19:52:38 -080083 return "OS Resource error (no memory ?)";
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 case 0x1109:
Joe Perches475be4d2012-02-19 19:52:38 -080085 return "CAPI not installed";
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 case 0x110A:
Joe Perches475be4d2012-02-19 19:52:38 -080087 return "Controller does not support external equipment";
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 case 0x110B:
Joe Perches475be4d2012-02-19 19:52:38 -080089 return "Controller does only support external equipment";
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/*-- error information concerning resource / coding problems -----*/
92 case 0x2001:
Joe Perches475be4d2012-02-19 19:52:38 -080093 return "Message not supported in current state";
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 case 0x2002:
Joe Perches475be4d2012-02-19 19:52:38 -080095 return "Illegal Controller / PLCI / NCCI";
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 case 0x2003:
Joe Perches475be4d2012-02-19 19:52:38 -080097 return "Out of PLCI";
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 case 0x2004:
Joe Perches475be4d2012-02-19 19:52:38 -080099 return "Out of NCCI";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 case 0x2005:
Joe Perches475be4d2012-02-19 19:52:38 -0800101 return "Out of LISTEN";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 case 0x2006:
Joe Perches475be4d2012-02-19 19:52:38 -0800103 return "Out of FAX resources (protocol T.30)";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 case 0x2007:
Joe Perches475be4d2012-02-19 19:52:38 -0800105 return "Illegal message parameter coding";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/*-- error information concerning requested services -----*/
108 case 0x3001:
Joe Perches475be4d2012-02-19 19:52:38 -0800109 return "B1 protocol not supported";
110 case 0x3002:
111 return "B2 protocol not supported";
112 case 0x3003:
113 return "B3 protocol not supported";
114 case 0x3004:
115 return "B1 protocol parameter not supported";
116 case 0x3005:
117 return "B2 protocol parameter not supported";
118 case 0x3006:
119 return "B3 protocol parameter not supported";
120 case 0x3007:
121 return "B protocol combination not supported";
122 case 0x3008:
123 return "NCPI not supported";
124 case 0x3009:
125 return "CIP Value unknown";
126 case 0x300A:
127 return "Flags not supported (reserved bits)";
128 case 0x300B:
129 return "Facility not supported";
130 case 0x300C:
131 return "Data length not supported by current protocol";
132 case 0x300D:
133 return "Reset procedure not supported by current protocol";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135/*-- informations about the clearing of a physical connection -----*/
Joe Perches475be4d2012-02-19 19:52:38 -0800136 case 0x3301:
137 return "Protocol error layer 1 (broken line or B-channel removed by signalling protocol)";
138 case 0x3302:
139 return "Protocol error layer 2";
140 case 0x3303:
141 return "Protocol error layer 3";
142 case 0x3304:
143 return "Another application got that call";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/*-- T.30 specific reasons -----*/
Joe Perches475be4d2012-02-19 19:52:38 -0800145 case 0x3311:
146 return "Connecting not successful (remote station is no FAX G3 machine)";
147 case 0x3312:
148 return "Connecting not successful (training error)";
149 case 0x3313:
150 return "Disconnected before transfer (remote station does not support transfer mode, e.g. resolution)";
151 case 0x3314:
152 return "Disconnected during transfer (remote abort)";
153 case 0x3315:
154 return "Disconnected during transfer (remote procedure error, e.g. unsuccessful repetition of T.30 commands)";
155 case 0x3316:
156 return "Disconnected during transfer (local tx data underrun)";
157 case 0x3317:
158 return "Disconnected during transfer (local rx data overflow)";
159 case 0x3318:
160 return "Disconnected during transfer (local abort)";
161 case 0x3319:
162 return "Illegal parameter coding (e.g. SFF coding error)";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164/*-- disconnect causes from the network according to ETS 300 102-1/Q.931 -----*/
165 case 0x3481: return "Unallocated (unassigned) number";
166 case 0x3482: return "No route to specified transit network";
167 case 0x3483: return "No route to destination";
168 case 0x3486: return "Channel unacceptable";
Joe Perches475be4d2012-02-19 19:52:38 -0800169 case 0x3487:
170 return "Call awarded and being delivered in an established channel";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 case 0x3490: return "Normal call clearing";
172 case 0x3491: return "User busy";
173 case 0x3492: return "No user responding";
174 case 0x3493: return "No answer from user (user alerted)";
175 case 0x3495: return "Call rejected";
176 case 0x3496: return "Number changed";
177 case 0x349A: return "Non-selected user clearing";
178 case 0x349B: return "Destination out of order";
179 case 0x349C: return "Invalid number format";
180 case 0x349D: return "Facility rejected";
181 case 0x349E: return "Response to STATUS ENQUIRY";
182 case 0x349F: return "Normal, unspecified";
183 case 0x34A2: return "No circuit / channel available";
184 case 0x34A6: return "Network out of order";
185 case 0x34A9: return "Temporary failure";
186 case 0x34AA: return "Switching equipment congestion";
187 case 0x34AB: return "Access information discarded";
188 case 0x34AC: return "Requested circuit / channel not available";
189 case 0x34AF: return "Resources unavailable, unspecified";
190 case 0x34B1: return "Quality of service unavailable";
191 case 0x34B2: return "Requested facility not subscribed";
192 case 0x34B9: return "Bearer capability not authorized";
193 case 0x34BA: return "Bearer capability not presently available";
194 case 0x34BF: return "Service or option not available, unspecified";
195 case 0x34C1: return "Bearer capability not implemented";
196 case 0x34C2: return "Channel type not implemented";
197 case 0x34C5: return "Requested facility not implemented";
198 case 0x34C6: return "Only restricted digital information bearer capability is available";
199 case 0x34CF: return "Service or option not implemented, unspecified";
200 case 0x34D1: return "Invalid call reference value";
201 case 0x34D2: return "Identified channel does not exist";
202 case 0x34D3: return "A suspended call exists, but this call identity does not";
203 case 0x34D4: return "Call identity in use";
204 case 0x34D5: return "No call suspended";
205 case 0x34D6: return "Call having the requested call identity has been cleared";
206 case 0x34D8: return "Incompatible destination";
207 case 0x34DB: return "Invalid transit network selection";
208 case 0x34DF: return "Invalid message, unspecified";
209 case 0x34E0: return "Mandatory information element is missing";
210 case 0x34E1: return "Message type non-existent or not implemented";
211 case 0x34E2: return "Message not compatible with call state or message type non-existent or not implemented";
212 case 0x34E3: return "Information element non-existent or not implemented";
213 case 0x34E4: return "Invalid information element contents";
214 case 0x34E5: return "Message not compatible with call state";
215 case 0x34E6: return "Recovery on timer expiry";
216 case 0x34EF: return "Protocol error, unspecified";
217 case 0x34FF: return "Interworking, unspecified";
218
219 default: return "No additional information";
Joe Perches475be4d2012-02-19 19:52:38 -0800220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222#endif
223
224typedef struct {
225 int typ;
226 size_t off;
227} _cdef;
228
229#define _CBYTE 1
230#define _CWORD 2
231#define _CDWORD 3
232#define _CSTRUCT 4
233#define _CMSTRUCT 5
234#define _CEND 6
235
236static _cdef cdef[] =
237{
Joe Perches475be4d2012-02-19 19:52:38 -0800238 /*00 */
239 {_CEND},
240 /*01 */
241 {_CEND},
242 /*02 */
243 {_CEND},
244 /*03 */
245 {_CDWORD, offsetof(_cmsg, adr.adrController)},
246 /*04 */
247 {_CMSTRUCT, offsetof(_cmsg, AdditionalInfo)},
248 /*05 */
249 {_CSTRUCT, offsetof(_cmsg, B1configuration)},
250 /*06 */
251 {_CWORD, offsetof(_cmsg, B1protocol)},
252 /*07 */
253 {_CSTRUCT, offsetof(_cmsg, B2configuration)},
254 /*08 */
255 {_CWORD, offsetof(_cmsg, B2protocol)},
256 /*09 */
257 {_CSTRUCT, offsetof(_cmsg, B3configuration)},
258 /*0a */
259 {_CWORD, offsetof(_cmsg, B3protocol)},
260 /*0b */
261 {_CSTRUCT, offsetof(_cmsg, BC)},
262 /*0c */
263 {_CSTRUCT, offsetof(_cmsg, BChannelinformation)},
264 /*0d */
265 {_CMSTRUCT, offsetof(_cmsg, BProtocol)},
266 /*0e */
267 {_CSTRUCT, offsetof(_cmsg, CalledPartyNumber)},
268 /*0f */
269 {_CSTRUCT, offsetof(_cmsg, CalledPartySubaddress)},
270 /*10 */
271 {_CSTRUCT, offsetof(_cmsg, CallingPartyNumber)},
272 /*11 */
273 {_CSTRUCT, offsetof(_cmsg, CallingPartySubaddress)},
274 /*12 */
275 {_CDWORD, offsetof(_cmsg, CIPmask)},
276 /*13 */
277 {_CDWORD, offsetof(_cmsg, CIPmask2)},
278 /*14 */
279 {_CWORD, offsetof(_cmsg, CIPValue)},
280 /*15 */
281 {_CDWORD, offsetof(_cmsg, Class)},
282 /*16 */
283 {_CSTRUCT, offsetof(_cmsg, ConnectedNumber)},
284 /*17 */
285 {_CSTRUCT, offsetof(_cmsg, ConnectedSubaddress)},
286 /*18 */
287 {_CDWORD, offsetof(_cmsg, Data)},
288 /*19 */
289 {_CWORD, offsetof(_cmsg, DataHandle)},
290 /*1a */
291 {_CWORD, offsetof(_cmsg, DataLength)},
292 /*1b */
293 {_CSTRUCT, offsetof(_cmsg, FacilityConfirmationParameter)},
294 /*1c */
295 {_CSTRUCT, offsetof(_cmsg, Facilitydataarray)},
296 /*1d */
297 {_CSTRUCT, offsetof(_cmsg, FacilityIndicationParameter)},
298 /*1e */
299 {_CSTRUCT, offsetof(_cmsg, FacilityRequestParameter)},
300 /*1f */
301 {_CWORD, offsetof(_cmsg, FacilitySelector)},
302 /*20 */
303 {_CWORD, offsetof(_cmsg, Flags)},
304 /*21 */
305 {_CDWORD, offsetof(_cmsg, Function)},
306 /*22 */
307 {_CSTRUCT, offsetof(_cmsg, HLC)},
308 /*23 */
309 {_CWORD, offsetof(_cmsg, Info)},
310 /*24 */
311 {_CSTRUCT, offsetof(_cmsg, InfoElement)},
312 /*25 */
313 {_CDWORD, offsetof(_cmsg, InfoMask)},
314 /*26 */
315 {_CWORD, offsetof(_cmsg, InfoNumber)},
316 /*27 */
317 {_CSTRUCT, offsetof(_cmsg, Keypadfacility)},
318 /*28 */
319 {_CSTRUCT, offsetof(_cmsg, LLC)},
320 /*29 */
321 {_CSTRUCT, offsetof(_cmsg, ManuData)},
322 /*2a */
323 {_CDWORD, offsetof(_cmsg, ManuID)},
324 /*2b */
325 {_CSTRUCT, offsetof(_cmsg, NCPI)},
326 /*2c */
327 {_CWORD, offsetof(_cmsg, Reason)},
328 /*2d */
329 {_CWORD, offsetof(_cmsg, Reason_B3)},
330 /*2e */
331 {_CWORD, offsetof(_cmsg, Reject)},
332 /*2f */
333 {_CSTRUCT, offsetof(_cmsg, Useruserdata)}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334};
335
336static unsigned char *cpars[] =
337{
Joe Perches475be4d2012-02-19 19:52:38 -0800338 /* ALERT_REQ */ [0x01] = "\x03\x04\x0c\x27\x2f\x1c\x01\x01",
339 /* CONNECT_REQ */ [0x02] = "\x03\x14\x0e\x10\x0f\x11\x0d\x06\x08\x0a\x05\x07\x09\x01\x0b\x28\x22\x04\x0c\x27\x2f\x1c\x01\x01",
340 /* DISCONNECT_REQ */ [0x04] = "\x03\x04\x0c\x27\x2f\x1c\x01\x01",
341 /* LISTEN_REQ */ [0x05] = "\x03\x25\x12\x13\x10\x11\x01",
342 /* INFO_REQ */ [0x08] = "\x03\x0e\x04\x0c\x27\x2f\x1c\x01\x01",
343 /* FACILITY_REQ */ [0x09] = "\x03\x1f\x1e\x01",
344 /* SELECT_B_PROTOCOL_REQ */ [0x0a] = "\x03\x0d\x06\x08\x0a\x05\x07\x09\x01\x01",
345 /* CONNECT_B3_REQ */ [0x0b] = "\x03\x2b\x01",
346 /* DISCONNECT_B3_REQ */ [0x0d] = "\x03\x2b\x01",
347 /* DATA_B3_REQ */ [0x0f] = "\x03\x18\x1a\x19\x20\x01",
348 /* RESET_B3_REQ */ [0x10] = "\x03\x2b\x01",
349 /* ALERT_CONF */ [0x13] = "\x03\x23\x01",
350 /* CONNECT_CONF */ [0x14] = "\x03\x23\x01",
351 /* DISCONNECT_CONF */ [0x16] = "\x03\x23\x01",
352 /* LISTEN_CONF */ [0x17] = "\x03\x23\x01",
353 /* MANUFACTURER_REQ */ [0x18] = "\x03\x2a\x15\x21\x29\x01",
354 /* INFO_CONF */ [0x1a] = "\x03\x23\x01",
355 /* FACILITY_CONF */ [0x1b] = "\x03\x23\x1f\x1b\x01",
356 /* SELECT_B_PROTOCOL_CONF */ [0x1c] = "\x03\x23\x01",
357 /* CONNECT_B3_CONF */ [0x1d] = "\x03\x23\x01",
358 /* DISCONNECT_B3_CONF */ [0x1f] = "\x03\x23\x01",
359 /* DATA_B3_CONF */ [0x21] = "\x03\x19\x23\x01",
360 /* RESET_B3_CONF */ [0x22] = "\x03\x23\x01",
361 /* CONNECT_IND */ [0x26] = "\x03\x14\x0e\x10\x0f\x11\x0b\x28\x22\x04\x0c\x27\x2f\x1c\x01\x01",
362 /* CONNECT_ACTIVE_IND */ [0x27] = "\x03\x16\x17\x28\x01",
363 /* DISCONNECT_IND */ [0x28] = "\x03\x2c\x01",
364 /* MANUFACTURER_CONF */ [0x2a] = "\x03\x2a\x15\x21\x29\x01",
365 /* INFO_IND */ [0x2c] = "\x03\x26\x24\x01",
366 /* FACILITY_IND */ [0x2d] = "\x03\x1f\x1d\x01",
367 /* CONNECT_B3_IND */ [0x2f] = "\x03\x2b\x01",
368 /* CONNECT_B3_ACTIVE_IND */ [0x30] = "\x03\x2b\x01",
369 /* DISCONNECT_B3_IND */ [0x31] = "\x03\x2d\x2b\x01",
370 /* DATA_B3_IND */ [0x33] = "\x03\x18\x1a\x19\x20\x01",
371 /* RESET_B3_IND */ [0x34] = "\x03\x2b\x01",
372 /* CONNECT_B3_T90_ACTIVE_IND */ [0x35] = "\x03\x2b\x01",
373 /* CONNECT_RESP */ [0x38] = "\x03\x2e\x0d\x06\x08\x0a\x05\x07\x09\x01\x16\x17\x28\x04\x0c\x27\x2f\x1c\x01\x01",
374 /* CONNECT_ACTIVE_RESP */ [0x39] = "\x03\x01",
375 /* DISCONNECT_RESP */ [0x3a] = "\x03\x01",
376 /* MANUFACTURER_IND */ [0x3c] = "\x03\x2a\x15\x21\x29\x01",
377 /* INFO_RESP */ [0x3e] = "\x03\x01",
378 /* FACILITY_RESP */ [0x3f] = "\x03\x1f\x01",
379 /* CONNECT_B3_RESP */ [0x41] = "\x03\x2e\x2b\x01",
380 /* CONNECT_B3_ACTIVE_RESP */ [0x42] = "\x03\x01",
381 /* DISCONNECT_B3_RESP */ [0x43] = "\x03\x01",
382 /* DATA_B3_RESP */ [0x45] = "\x03\x19\x01",
383 /* RESET_B3_RESP */ [0x46] = "\x03\x01",
384 /* CONNECT_B3_T90_ACTIVE_RESP */ [0x47] = "\x03\x01",
385 /* MANUFACTURER_RESP */ [0x4e] = "\x03\x2a\x15\x21\x29\x01",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386};
387
388/*-------------------------------------------------------*/
389
Joe Perches475be4d2012-02-19 19:52:38 -0800390#define byteTLcpy(x, y) *(u8 *)(x) = *(u8 *)(y);
391#define wordTLcpy(x, y) *(u16 *)(x) = *(u16 *)(y);
392#define dwordTLcpy(x, y) memcpy(x, y, 4);
393#define structTLcpy(x, y, l) memcpy(x, y, l)
394#define structTLcpyovl(x, y, l) memmove(x, y, l)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Joe Perches475be4d2012-02-19 19:52:38 -0800396#define byteTRcpy(x, y) *(u8 *)(y) = *(u8 *)(x);
397#define wordTRcpy(x, y) *(u16 *)(y) = *(u16 *)(x);
398#define dwordTRcpy(x, y) memcpy(y, x, 4);
399#define structTRcpy(x, y, l) memcpy(y, x, l)
400#define structTRcpyovl(x, y, l) memmove(y, x, l)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402/*-------------------------------------------------------*/
403static unsigned command_2_index(unsigned c, unsigned sc)
404{
405 if (c & 0x80)
406 c = 0x9 + (c & 0x0f);
407 else if (c <= 0x0f);
408 else if (c == 0x41)
409 c = 0x9 + 0x1;
410 else if (c == 0xff)
411 c = 0x00;
412 return (sc & 3) * (0x9 + 0x9) + c;
413}
414
415/*-------------------------------------------------------*/
416#define TYP (cdef[cmsg->par[cmsg->p]].typ)
Joe Perches475be4d2012-02-19 19:52:38 -0800417#define OFF (((u8 *)cmsg) + cdef[cmsg->par[cmsg->p]].off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Joe Perches475be4d2012-02-19 19:52:38 -0800419static void jumpcstruct(_cmsg *cmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 unsigned layer;
422 for (cmsg->p++, layer = 1; layer;) {
423 /* $$$$$ assert (cmsg->p); */
424 cmsg->p++;
425 switch (TYP) {
426 case _CMSTRUCT:
427 layer++;
428 break;
429 case _CEND:
430 layer--;
431 break;
432 }
433 }
434}
435/*-------------------------------------------------------*/
Joe Perches475be4d2012-02-19 19:52:38 -0800436static void pars_2_message(_cmsg *cmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438
439 for (; TYP != _CEND; cmsg->p++) {
440 switch (TYP) {
441 case _CBYTE:
442 byteTLcpy(cmsg->m + cmsg->l, OFF);
443 cmsg->l++;
444 break;
445 case _CWORD:
446 wordTLcpy(cmsg->m + cmsg->l, OFF);
447 cmsg->l += 2;
448 break;
449 case _CDWORD:
450 dwordTLcpy(cmsg->m + cmsg->l, OFF);
451 cmsg->l += 4;
452 break;
453 case _CSTRUCT:
Harvey Harrison2f9e9b62008-04-28 02:14:37 -0700454 if (*(u8 **) OFF == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 *(cmsg->m + cmsg->l) = '\0';
456 cmsg->l++;
457 } else if (**(_cstruct *) OFF != 0xff) {
458 structTLcpy(cmsg->m + cmsg->l, *(_cstruct *) OFF, 1 + **(_cstruct *) OFF);
459 cmsg->l += 1 + **(_cstruct *) OFF;
460 } else {
461 _cstruct s = *(_cstruct *) OFF;
462 structTLcpy(cmsg->m + cmsg->l, s, 3 + *(u16 *) (s + 1));
463 cmsg->l += 3 + *(u16 *) (s + 1);
464 }
465 break;
466 case _CMSTRUCT:
467/*----- Metastruktur 0 -----*/
468 if (*(_cmstruct *) OFF == CAPI_DEFAULT) {
469 *(cmsg->m + cmsg->l) = '\0';
470 cmsg->l++;
471 jumpcstruct(cmsg);
472 }
473/*----- Metastruktur wird composed -----*/
474 else {
475 unsigned _l = cmsg->l;
476 unsigned _ls;
477 cmsg->l++;
478 cmsg->p++;
479 pars_2_message(cmsg);
480 _ls = cmsg->l - _l - 1;
481 if (_ls < 255)
482 (cmsg->m + _l)[0] = (u8) _ls;
483 else {
484 structTLcpyovl(cmsg->m + _l + 3, cmsg->m + _l + 1, _ls);
485 (cmsg->m + _l)[0] = 0xff;
486 wordTLcpy(cmsg->m + _l + 1, &_ls);
487 }
488 }
489 break;
490 }
491 }
492}
493
Tilman Schmidt4793d152009-06-07 09:09:23 +0000494/**
495 * capi_cmsg2message() - assemble CAPI 2.0 message from _cmsg structure
496 * @cmsg: _cmsg structure
497 * @msg: buffer for assembled message
498 *
499 * Return value: 0 for success
500 */
501
Joe Perches475be4d2012-02-19 19:52:38 -0800502unsigned capi_cmsg2message(_cmsg *cmsg, u8 *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 cmsg->m = msg;
505 cmsg->l = 8;
506 cmsg->p = 0;
507 cmsg->par = cpars[command_2_index(cmsg->Command, cmsg->Subcommand)];
508
509 pars_2_message(cmsg);
510
511 wordTLcpy(msg + 0, &cmsg->l);
512 byteTLcpy(cmsg->m + 4, &cmsg->Command);
513 byteTLcpy(cmsg->m + 5, &cmsg->Subcommand);
514 wordTLcpy(cmsg->m + 2, &cmsg->ApplId);
515 wordTLcpy(cmsg->m + 6, &cmsg->Messagenumber);
516
517 return 0;
518}
519
520/*-------------------------------------------------------*/
Joe Perches475be4d2012-02-19 19:52:38 -0800521static void message_2_pars(_cmsg *cmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 for (; TYP != _CEND; cmsg->p++) {
524
525 switch (TYP) {
526 case _CBYTE:
527 byteTRcpy(cmsg->m + cmsg->l, OFF);
528 cmsg->l++;
529 break;
530 case _CWORD:
531 wordTRcpy(cmsg->m + cmsg->l, OFF);
532 cmsg->l += 2;
533 break;
534 case _CDWORD:
535 dwordTRcpy(cmsg->m + cmsg->l, OFF);
536 cmsg->l += 4;
537 break;
538 case _CSTRUCT:
539 *(u8 **) OFF = cmsg->m + cmsg->l;
540
541 if (cmsg->m[cmsg->l] != 0xff)
542 cmsg->l += 1 + cmsg->m[cmsg->l];
543 else
544 cmsg->l += 3 + *(u16 *) (cmsg->m + cmsg->l + 1);
545 break;
546 case _CMSTRUCT:
547/*----- Metastruktur 0 -----*/
548 if (cmsg->m[cmsg->l] == '\0') {
549 *(_cmstruct *) OFF = CAPI_DEFAULT;
550 cmsg->l++;
551 jumpcstruct(cmsg);
552 } else {
553 unsigned _l = cmsg->l;
554 *(_cmstruct *) OFF = CAPI_COMPOSE;
555 cmsg->l = (cmsg->m + _l)[0] == 255 ? cmsg->l + 3 : cmsg->l + 1;
556 cmsg->p++;
557 message_2_pars(cmsg);
558 }
559 break;
560 }
561 }
562}
563
Tilman Schmidt4793d152009-06-07 09:09:23 +0000564/**
565 * capi_message2cmsg() - disassemble CAPI 2.0 message into _cmsg structure
566 * @cmsg: _cmsg structure
567 * @msg: buffer for assembled message
568 *
569 * Return value: 0 for success
570 */
571
Joe Perches475be4d2012-02-19 19:52:38 -0800572unsigned capi_message2cmsg(_cmsg *cmsg, u8 *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 memset(cmsg, 0, sizeof(_cmsg));
575 cmsg->m = msg;
576 cmsg->l = 8;
577 cmsg->p = 0;
578 byteTRcpy(cmsg->m + 4, &cmsg->Command);
579 byteTRcpy(cmsg->m + 5, &cmsg->Subcommand);
580 cmsg->par = cpars[command_2_index(cmsg->Command, cmsg->Subcommand)];
581
582 message_2_pars(cmsg);
583
584 wordTRcpy(msg + 0, &cmsg->l);
585 wordTRcpy(cmsg->m + 2, &cmsg->ApplId);
586 wordTRcpy(cmsg->m + 6, &cmsg->Messagenumber);
587
588 return 0;
589}
590
Tilman Schmidt4793d152009-06-07 09:09:23 +0000591/**
592 * capi_cmsg_header() - initialize header part of _cmsg structure
593 * @cmsg: _cmsg structure
594 * @_ApplId: ApplID field value
595 * @_Command: Command field value
596 * @_Subcommand: Subcommand field value
597 * @_Messagenumber: Message Number field value
598 * @_Controller: Controller/PLCI/NCCI field value
599 *
600 * Return value: 0 for success
601 */
602
Joe Perches475be4d2012-02-19 19:52:38 -0800603unsigned capi_cmsg_header(_cmsg *cmsg, u16 _ApplId,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 u8 _Command, u8 _Subcommand,
605 u16 _Messagenumber, u32 _Controller)
606{
607 memset(cmsg, 0, sizeof(_cmsg));
608 cmsg->ApplId = _ApplId;
609 cmsg->Command = _Command;
610 cmsg->Subcommand = _Subcommand;
611 cmsg->Messagenumber = _Messagenumber;
612 cmsg->adr.adrController = _Controller;
613 return 0;
614}
615
616/*-------------------------------------------------------*/
617
618static char *mnames[] =
619{
620 [0x01] = "ALERT_REQ",
621 [0x02] = "CONNECT_REQ",
622 [0x04] = "DISCONNECT_REQ",
623 [0x05] = "LISTEN_REQ",
624 [0x08] = "INFO_REQ",
625 [0x09] = "FACILITY_REQ",
626 [0x0a] = "SELECT_B_PROTOCOL_REQ",
627 [0x0b] = "CONNECT_B3_REQ",
628 [0x0d] = "DISCONNECT_B3_REQ",
629 [0x0f] = "DATA_B3_REQ",
630 [0x10] = "RESET_B3_REQ",
631 [0x13] = "ALERT_CONF",
632 [0x14] = "CONNECT_CONF",
633 [0x16] = "DISCONNECT_CONF",
634 [0x17] = "LISTEN_CONF",
635 [0x18] = "MANUFACTURER_REQ",
636 [0x1a] = "INFO_CONF",
637 [0x1b] = "FACILITY_CONF",
638 [0x1c] = "SELECT_B_PROTOCOL_CONF",
639 [0x1d] = "CONNECT_B3_CONF",
640 [0x1f] = "DISCONNECT_B3_CONF",
641 [0x21] = "DATA_B3_CONF",
642 [0x22] = "RESET_B3_CONF",
643 [0x26] = "CONNECT_IND",
644 [0x27] = "CONNECT_ACTIVE_IND",
645 [0x28] = "DISCONNECT_IND",
646 [0x2a] = "MANUFACTURER_CONF",
647 [0x2c] = "INFO_IND",
648 [0x2d] = "FACILITY_IND",
649 [0x2f] = "CONNECT_B3_IND",
650 [0x30] = "CONNECT_B3_ACTIVE_IND",
651 [0x31] = "DISCONNECT_B3_IND",
652 [0x33] = "DATA_B3_IND",
653 [0x34] = "RESET_B3_IND",
654 [0x35] = "CONNECT_B3_T90_ACTIVE_IND",
655 [0x38] = "CONNECT_RESP",
656 [0x39] = "CONNECT_ACTIVE_RESP",
657 [0x3a] = "DISCONNECT_RESP",
658 [0x3c] = "MANUFACTURER_IND",
659 [0x3e] = "INFO_RESP",
660 [0x3f] = "FACILITY_RESP",
661 [0x41] = "CONNECT_B3_RESP",
662 [0x42] = "CONNECT_B3_ACTIVE_RESP",
663 [0x43] = "DISCONNECT_B3_RESP",
664 [0x45] = "DATA_B3_RESP",
665 [0x46] = "RESET_B3_RESP",
666 [0x47] = "CONNECT_B3_T90_ACTIVE_RESP",
667 [0x4e] = "MANUFACTURER_RESP"
668};
669
Tilman Schmidt4793d152009-06-07 09:09:23 +0000670/**
671 * capi_cmd2str() - convert CAPI 2.0 command/subcommand number to name
672 * @cmd: command number
673 * @subcmd: subcommand number
674 *
675 * Return value: static string, NULL if command/subcommand unknown
676 */
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678char *capi_cmd2str(u8 cmd, u8 subcmd)
679{
680 return mnames[command_2_index(cmd, subcmd)];
681}
682
683
684/*-------------------------------------------------------*/
Karsten Keil17f0cd22007-02-28 20:13:50 -0800685
686#ifdef CONFIG_CAPI_TRACE
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688/*-------------------------------------------------------*/
689
690static char *pnames[] =
691{
Joe Perches475be4d2012-02-19 19:52:38 -0800692 /*00 */ NULL,
693 /*01 */ NULL,
694 /*02 */ NULL,
695 /*03 */ "Controller/PLCI/NCCI",
696 /*04 */ "AdditionalInfo",
697 /*05 */ "B1configuration",
698 /*06 */ "B1protocol",
699 /*07 */ "B2configuration",
700 /*08 */ "B2protocol",
701 /*09 */ "B3configuration",
702 /*0a */ "B3protocol",
703 /*0b */ "BC",
704 /*0c */ "BChannelinformation",
705 /*0d */ "BProtocol",
706 /*0e */ "CalledPartyNumber",
707 /*0f */ "CalledPartySubaddress",
708 /*10 */ "CallingPartyNumber",
709 /*11 */ "CallingPartySubaddress",
710 /*12 */ "CIPmask",
711 /*13 */ "CIPmask2",
712 /*14 */ "CIPValue",
713 /*15 */ "Class",
714 /*16 */ "ConnectedNumber",
715 /*17 */ "ConnectedSubaddress",
716 /*18 */ "Data32",
717 /*19 */ "DataHandle",
718 /*1a */ "DataLength",
719 /*1b */ "FacilityConfirmationParameter",
720 /*1c */ "Facilitydataarray",
721 /*1d */ "FacilityIndicationParameter",
722 /*1e */ "FacilityRequestParameter",
723 /*1f */ "FacilitySelector",
724 /*20 */ "Flags",
725 /*21 */ "Function",
726 /*22 */ "HLC",
727 /*23 */ "Info",
728 /*24 */ "InfoElement",
729 /*25 */ "InfoMask",
730 /*26 */ "InfoNumber",
731 /*27 */ "Keypadfacility",
732 /*28 */ "LLC",
733 /*29 */ "ManuData",
734 /*2a */ "ManuID",
735 /*2b */ "NCPI",
736 /*2c */ "Reason",
737 /*2d */ "Reason_B3",
738 /*2e */ "Reject",
739 /*2f */ "Useruserdata"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740};
741
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744#include <stdarg.h>
745
746/*-------------------------------------------------------*/
Joe Perches475be4d2012-02-19 19:52:38 -0800747static _cdebbuf *bufprint(_cdebbuf *cdb, char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 va_list f;
Joe Perches475be4d2012-02-19 19:52:38 -0800750 size_t n, r;
Karsten Keil17f0cd22007-02-28 20:13:50 -0800751
752 if (!cdb)
753 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 va_start(f, fmt);
Karsten Keil17f0cd22007-02-28 20:13:50 -0800755 r = cdb->size - cdb->pos;
756 n = vsnprintf(cdb->p, r, fmt, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 va_end(f);
Karsten Keil17f0cd22007-02-28 20:13:50 -0800758 if (n >= r) {
759 /* truncated, need bigger buffer */
760 size_t ns = 2 * cdb->size;
761 u_char *nb;
762
763 while ((ns - cdb->pos) <= n)
764 ns *= 2;
765 nb = kmalloc(ns, GFP_ATOMIC);
766 if (!nb) {
767 cdebbuf_free(cdb);
768 return NULL;
769 }
770 memcpy(nb, cdb->buf, cdb->pos);
771 kfree(cdb->buf);
772 nb[cdb->pos] = 0;
773 cdb->buf = nb;
774 cdb->p = cdb->buf + cdb->pos;
775 cdb->size = ns;
776 va_start(f, fmt);
777 r = cdb->size - cdb->pos;
778 n = vsnprintf(cdb->p, r, fmt, f);
779 va_end(f);
780 }
781 cdb->p += n;
782 cdb->pos += n;
783 return cdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Joe Perches475be4d2012-02-19 19:52:38 -0800786static _cdebbuf *printstructlen(_cdebbuf *cdb, u8 *m, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 unsigned hex = 0;
Karsten Keil17f0cd22007-02-28 20:13:50 -0800789
790 if (!cdb)
791 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 for (; len; len--, m++)
793 if (isalnum(*m) || *m == ' ') {
794 if (hex)
Karsten Keil17f0cd22007-02-28 20:13:50 -0800795 cdb = bufprint(cdb, ">");
796 cdb = bufprint(cdb, "%c", *m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 hex = 0;
798 } else {
799 if (!hex)
Karsten Keil17f0cd22007-02-28 20:13:50 -0800800 cdb = bufprint(cdb, "<%02x", *m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 else
Karsten Keil17f0cd22007-02-28 20:13:50 -0800802 cdb = bufprint(cdb, " %02x", *m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 hex = 1;
804 }
805 if (hex)
Karsten Keil17f0cd22007-02-28 20:13:50 -0800806 cdb = bufprint(cdb, ">");
807 return cdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
Joe Perches475be4d2012-02-19 19:52:38 -0800810static _cdebbuf *printstruct(_cdebbuf *cdb, u8 *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 unsigned len;
Karsten Keil17f0cd22007-02-28 20:13:50 -0800813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (m[0] != 0xff) {
815 len = m[0];
816 m += 1;
817 } else {
818 len = ((u16 *) (m + 1))[0];
819 m += 3;
820 }
Karsten Keil17f0cd22007-02-28 20:13:50 -0800821 cdb = printstructlen(cdb, m, len);
822 return cdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
825/*-------------------------------------------------------*/
826#define NAME (pnames[cmsg->par[cmsg->p]])
827
Karsten Keil17f0cd22007-02-28 20:13:50 -0800828static _cdebbuf *protocol_message_2_pars(_cdebbuf *cdb, _cmsg *cmsg, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 for (; TYP != _CEND; cmsg->p++) {
831 int slen = 29 + 3 - level;
832 int i;
833
Karsten Keil17f0cd22007-02-28 20:13:50 -0800834 if (!cdb)
835 return NULL;
836 cdb = bufprint(cdb, " ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 for (i = 0; i < level - 1; i++)
Karsten Keil17f0cd22007-02-28 20:13:50 -0800838 cdb = bufprint(cdb, " ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 switch (TYP) {
841 case _CBYTE:
Karsten Keil17f0cd22007-02-28 20:13:50 -0800842 cdb = bufprint(cdb, "%-*s = 0x%x\n", slen, NAME, *(u8 *) (cmsg->m + cmsg->l));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 cmsg->l++;
844 break;
845 case _CWORD:
Karsten Keil17f0cd22007-02-28 20:13:50 -0800846 cdb = bufprint(cdb, "%-*s = 0x%x\n", slen, NAME, *(u16 *) (cmsg->m + cmsg->l));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 cmsg->l += 2;
848 break;
849 case _CDWORD:
Karsten Keil17f0cd22007-02-28 20:13:50 -0800850 cdb = bufprint(cdb, "%-*s = 0x%lx\n", slen, NAME, *(u32 *) (cmsg->m + cmsg->l));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 cmsg->l += 4;
852 break;
853 case _CSTRUCT:
Karsten Keil17f0cd22007-02-28 20:13:50 -0800854 cdb = bufprint(cdb, "%-*s = ", slen, NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (cmsg->m[cmsg->l] == '\0')
Karsten Keil17f0cd22007-02-28 20:13:50 -0800856 cdb = bufprint(cdb, "default");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 else
Karsten Keil17f0cd22007-02-28 20:13:50 -0800858 cdb = printstruct(cdb, cmsg->m + cmsg->l);
859 cdb = bufprint(cdb, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (cmsg->m[cmsg->l] != 0xff)
861 cmsg->l += 1 + cmsg->m[cmsg->l];
862 else
863 cmsg->l += 3 + *(u16 *) (cmsg->m + cmsg->l + 1);
864
865 break;
866
867 case _CMSTRUCT:
868/*----- Metastruktur 0 -----*/
869 if (cmsg->m[cmsg->l] == '\0') {
Karsten Keil17f0cd22007-02-28 20:13:50 -0800870 cdb = bufprint(cdb, "%-*s = default\n", slen, NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 cmsg->l++;
872 jumpcstruct(cmsg);
873 } else {
874 char *name = NAME;
875 unsigned _l = cmsg->l;
Karsten Keil17f0cd22007-02-28 20:13:50 -0800876 cdb = bufprint(cdb, "%-*s\n", slen, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 cmsg->l = (cmsg->m + _l)[0] == 255 ? cmsg->l + 3 : cmsg->l + 1;
878 cmsg->p++;
Karsten Keil17f0cd22007-02-28 20:13:50 -0800879 cdb = protocol_message_2_pars(cdb, cmsg, level + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881 break;
882 }
883 }
Karsten Keil17f0cd22007-02-28 20:13:50 -0800884 return cdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886/*-------------------------------------------------------*/
Karsten Keil17f0cd22007-02-28 20:13:50 -0800887
888static _cdebbuf *g_debbuf;
889static u_long g_debbuf_lock;
890static _cmsg *g_cmsg;
891
Adrian Bunk3a3a51d2007-05-08 00:32:33 -0700892static _cdebbuf *cdebbuf_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Karsten Keil17f0cd22007-02-28 20:13:50 -0800894 _cdebbuf *cdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Karsten Keil17f0cd22007-02-28 20:13:50 -0800896 if (likely(!test_and_set_bit(1, &g_debbuf_lock))) {
897 cdb = g_debbuf;
898 goto init;
899 } else
900 cdb = kmalloc(sizeof(_cdebbuf), GFP_ATOMIC);
901 if (!cdb)
902 return NULL;
903 cdb->buf = kmalloc(CDEBUG_SIZE, GFP_ATOMIC);
904 if (!cdb->buf) {
905 kfree(cdb);
906 return NULL;
907 }
908 cdb->size = CDEBUG_SIZE;
909init:
910 cdb->buf[0] = 0;
911 cdb->p = cdb->buf;
912 cdb->pos = 0;
913 return cdb;
914}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Tilman Schmidt4793d152009-06-07 09:09:23 +0000916/**
917 * cdebbuf_free() - free CAPI debug buffer
918 * @cdb: buffer to free
919 */
920
Karsten Keil17f0cd22007-02-28 20:13:50 -0800921void cdebbuf_free(_cdebbuf *cdb)
922{
923 if (likely(cdb == g_debbuf)) {
924 test_and_clear_bit(1, &g_debbuf_lock);
925 return;
926 }
927 if (likely(cdb))
928 kfree(cdb->buf);
929 kfree(cdb);
930}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Karsten Keil17f0cd22007-02-28 20:13:50 -0800932
Tilman Schmidt4793d152009-06-07 09:09:23 +0000933/**
934 * capi_message2str() - format CAPI 2.0 message for printing
935 * @msg: CAPI 2.0 message
936 *
937 * Allocates a CAPI debug buffer and fills it with a printable representation
938 * of the CAPI 2.0 message in @msg.
939 * Return value: allocated debug buffer, NULL on error
940 * The returned buffer should be freed by a call to cdebbuf_free() after use.
941 */
942
Joe Perches475be4d2012-02-19 19:52:38 -0800943_cdebbuf *capi_message2str(u8 *msg)
Karsten Keil17f0cd22007-02-28 20:13:50 -0800944{
945 _cdebbuf *cdb;
946 _cmsg *cmsg;
947
948 cdb = cdebbuf_alloc();
949 if (unlikely(!cdb))
950 return NULL;
951 if (likely(cdb == g_debbuf))
952 cmsg = g_cmsg;
953 else
954 cmsg = kmalloc(sizeof(_cmsg), GFP_ATOMIC);
955 if (unlikely(!cmsg)) {
956 cdebbuf_free(cdb);
957 return NULL;
958 }
959 cmsg->m = msg;
960 cmsg->l = 8;
961 cmsg->p = 0;
962 byteTRcpy(cmsg->m + 4, &cmsg->Command);
963 byteTRcpy(cmsg->m + 5, &cmsg->Subcommand);
964 cmsg->par = cpars[command_2_index(cmsg->Command, cmsg->Subcommand)];
965
966 cdb = bufprint(cdb, "%-26s ID=%03d #0x%04x LEN=%04d\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800967 mnames[command_2_index(cmsg->Command, cmsg->Subcommand)],
968 ((unsigned short *) msg)[1],
969 ((unsigned short *) msg)[3],
970 ((unsigned short *) msg)[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Karsten Keil17f0cd22007-02-28 20:13:50 -0800972 cdb = protocol_message_2_pars(cdb, cmsg, 1);
973 if (unlikely(cmsg != g_cmsg))
974 kfree(cmsg);
975 return cdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
Tilman Schmidt4793d152009-06-07 09:09:23 +0000978/**
979 * capi_cmsg2str() - format _cmsg structure for printing
980 * @cmsg: _cmsg structure
981 *
982 * Allocates a CAPI debug buffer and fills it with a printable representation
983 * of the CAPI 2.0 message stored in @cmsg by a previous call to
984 * capi_cmsg2message() or capi_message2cmsg().
985 * Return value: allocated debug buffer, NULL on error
986 * The returned buffer should be freed by a call to cdebbuf_free() after use.
987 */
988
Joe Perches475be4d2012-02-19 19:52:38 -0800989_cdebbuf *capi_cmsg2str(_cmsg *cmsg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Karsten Keil17f0cd22007-02-28 20:13:50 -0800991 _cdebbuf *cdb;
992
Tilman Schmidt7d31acd2009-06-07 09:09:24 +0000993 if (!cmsg->m)
994 return NULL; /* no message */
Karsten Keil17f0cd22007-02-28 20:13:50 -0800995 cdb = cdebbuf_alloc();
996 if (!cdb)
997 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 cmsg->l = 8;
999 cmsg->p = 0;
Karsten Keil17f0cd22007-02-28 20:13:50 -08001000 cdb = bufprint(cdb, "%s ID=%03d #0x%04x LEN=%04d\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001001 mnames[command_2_index(cmsg->Command, cmsg->Subcommand)],
1002 ((u16 *) cmsg->m)[1],
1003 ((u16 *) cmsg->m)[3],
1004 ((u16 *) cmsg->m)[0]);
Karsten Keil17f0cd22007-02-28 20:13:50 -08001005 cdb = protocol_message_2_pars(cdb, cmsg, 1);
1006 return cdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
Karsten Keil17f0cd22007-02-28 20:13:50 -08001009int __init cdebug_init(void)
1010{
Joe Perches475be4d2012-02-19 19:52:38 -08001011 g_cmsg = kmalloc(sizeof(_cmsg), GFP_KERNEL);
Karsten Keil17f0cd22007-02-28 20:13:50 -08001012 if (!g_cmsg)
Marcin Slusarz066b2112008-05-14 23:30:06 -07001013 return -ENOMEM;
Karsten Keil17f0cd22007-02-28 20:13:50 -08001014 g_debbuf = kmalloc(sizeof(_cdebbuf), GFP_KERNEL);
1015 if (!g_debbuf) {
1016 kfree(g_cmsg);
Marcin Slusarz066b2112008-05-14 23:30:06 -07001017 return -ENOMEM;
Karsten Keil17f0cd22007-02-28 20:13:50 -08001018 }
1019 g_debbuf->buf = kmalloc(CDEBUG_GSIZE, GFP_KERNEL);
1020 if (!g_debbuf->buf) {
1021 kfree(g_cmsg);
1022 kfree(g_debbuf);
Joe Perchesa419aef2009-08-18 11:18:35 -07001023 return -ENOMEM;
Karsten Keil17f0cd22007-02-28 20:13:50 -08001024 }
1025 g_debbuf->size = CDEBUG_GSIZE;
1026 g_debbuf->buf[0] = 0;
1027 g_debbuf->p = g_debbuf->buf;
1028 g_debbuf->pos = 0;
1029 return 0;
1030}
1031
1032void __exit cdebug_exit(void)
1033{
1034 if (g_debbuf)
1035 kfree(g_debbuf->buf);
1036 kfree(g_debbuf);
1037 kfree(g_cmsg);
1038}
1039
1040#else /* !CONFIG_CAPI_TRACE */
1041
1042static _cdebbuf g_debbuf = {"CONFIG_CAPI_TRACE not enabled", NULL, 0, 0};
1043
Joe Perches475be4d2012-02-19 19:52:38 -08001044_cdebbuf *capi_message2str(u8 *msg)
Karsten Keil17f0cd22007-02-28 20:13:50 -08001045{
1046 return &g_debbuf;
1047}
1048
Joe Perches475be4d2012-02-19 19:52:38 -08001049_cdebbuf *capi_cmsg2str(_cmsg *cmsg)
Karsten Keil17f0cd22007-02-28 20:13:50 -08001050{
1051 return &g_debbuf;
1052}
1053
Karsten Keil17f0cd22007-02-28 20:13:50 -08001054void cdebbuf_free(_cdebbuf *cdb)
1055{
1056}
1057
1058int __init cdebug_init(void)
1059{
1060 return 0;
1061}
1062
1063void __exit cdebug_exit(void)
1064{
1065}
1066
1067#endif
1068
Karsten Keil17f0cd22007-02-28 20:13:50 -08001069EXPORT_SYMBOL(cdebbuf_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070EXPORT_SYMBOL(capi_cmsg2message);
1071EXPORT_SYMBOL(capi_message2cmsg);
1072EXPORT_SYMBOL(capi_cmsg_header);
1073EXPORT_SYMBOL(capi_cmd2str);
1074EXPORT_SYMBOL(capi_cmsg2str);
1075EXPORT_SYMBOL(capi_message2str);
1076EXPORT_SYMBOL(capi_info2str);