capiutil.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /* $Id: capiutil.c,v 1.13.6.4 2001/09/23 22:24:33 kai Exp $
  2. *
  3. * CAPI 2.0 convert capi message to capi message struct
  4. *
  5. * From CAPI 2.0 Development Kit AVM 1995 (msg.c)
  6. * Rewritten for Linux 1996 by Carsten Paeth <[email protected]>
  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. #include <linux/module.h>
  13. #include <linux/string.h>
  14. #include <linux/ctype.h>
  15. #include <linux/stddef.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mm.h>
  18. #include <linux/init.h>
  19. #include <linux/isdn/capiutil.h>
  20. #include <linux/slab.h>
  21. #include "kcapi.h"
  22. /* from CAPI2.0 DDK AVM Berlin GmbH */
  23. typedef struct {
  24. int typ;
  25. size_t off;
  26. } _cdef;
  27. #define _CBYTE 1
  28. #define _CWORD 2
  29. #define _CDWORD 3
  30. #define _CSTRUCT 4
  31. #define _CMSTRUCT 5
  32. #define _CEND 6
  33. static _cdef cdef[] =
  34. {
  35. /*00 */
  36. {_CEND},
  37. /*01 */
  38. {_CEND},
  39. /*02 */
  40. {_CEND},
  41. /*03 */
  42. {_CDWORD, offsetof(_cmsg, adr.adrController)},
  43. /*04 */
  44. {_CMSTRUCT, offsetof(_cmsg, AdditionalInfo)},
  45. /*05 */
  46. {_CSTRUCT, offsetof(_cmsg, B1configuration)},
  47. /*06 */
  48. {_CWORD, offsetof(_cmsg, B1protocol)},
  49. /*07 */
  50. {_CSTRUCT, offsetof(_cmsg, B2configuration)},
  51. /*08 */
  52. {_CWORD, offsetof(_cmsg, B2protocol)},
  53. /*09 */
  54. {_CSTRUCT, offsetof(_cmsg, B3configuration)},
  55. /*0a */
  56. {_CWORD, offsetof(_cmsg, B3protocol)},
  57. /*0b */
  58. {_CSTRUCT, offsetof(_cmsg, BC)},
  59. /*0c */
  60. {_CSTRUCT, offsetof(_cmsg, BChannelinformation)},
  61. /*0d */
  62. {_CMSTRUCT, offsetof(_cmsg, BProtocol)},
  63. /*0e */
  64. {_CSTRUCT, offsetof(_cmsg, CalledPartyNumber)},
  65. /*0f */
  66. {_CSTRUCT, offsetof(_cmsg, CalledPartySubaddress)},
  67. /*10 */
  68. {_CSTRUCT, offsetof(_cmsg, CallingPartyNumber)},
  69. /*11 */
  70. {_CSTRUCT, offsetof(_cmsg, CallingPartySubaddress)},
  71. /*12 */
  72. {_CDWORD, offsetof(_cmsg, CIPmask)},
  73. /*13 */
  74. {_CDWORD, offsetof(_cmsg, CIPmask2)},
  75. /*14 */
  76. {_CWORD, offsetof(_cmsg, CIPValue)},
  77. /*15 */
  78. {_CDWORD, offsetof(_cmsg, Class)},
  79. /*16 */
  80. {_CSTRUCT, offsetof(_cmsg, ConnectedNumber)},
  81. /*17 */
  82. {_CSTRUCT, offsetof(_cmsg, ConnectedSubaddress)},
  83. /*18 */
  84. {_CDWORD, offsetof(_cmsg, Data)},
  85. /*19 */
  86. {_CWORD, offsetof(_cmsg, DataHandle)},
  87. /*1a */
  88. {_CWORD, offsetof(_cmsg, DataLength)},
  89. /*1b */
  90. {_CSTRUCT, offsetof(_cmsg, FacilityConfirmationParameter)},
  91. /*1c */
  92. {_CSTRUCT, offsetof(_cmsg, Facilitydataarray)},
  93. /*1d */
  94. {_CSTRUCT, offsetof(_cmsg, FacilityIndicationParameter)},
  95. /*1e */
  96. {_CSTRUCT, offsetof(_cmsg, FacilityRequestParameter)},
  97. /*1f */
  98. {_CWORD, offsetof(_cmsg, FacilitySelector)},
  99. /*20 */
  100. {_CWORD, offsetof(_cmsg, Flags)},
  101. /*21 */
  102. {_CDWORD, offsetof(_cmsg, Function)},
  103. /*22 */
  104. {_CSTRUCT, offsetof(_cmsg, HLC)},
  105. /*23 */
  106. {_CWORD, offsetof(_cmsg, Info)},
  107. /*24 */
  108. {_CSTRUCT, offsetof(_cmsg, InfoElement)},
  109. /*25 */
  110. {_CDWORD, offsetof(_cmsg, InfoMask)},
  111. /*26 */
  112. {_CWORD, offsetof(_cmsg, InfoNumber)},
  113. /*27 */
  114. {_CSTRUCT, offsetof(_cmsg, Keypadfacility)},
  115. /*28 */
  116. {_CSTRUCT, offsetof(_cmsg, LLC)},
  117. /*29 */
  118. {_CSTRUCT, offsetof(_cmsg, ManuData)},
  119. /*2a */
  120. {_CDWORD, offsetof(_cmsg, ManuID)},
  121. /*2b */
  122. {_CSTRUCT, offsetof(_cmsg, NCPI)},
  123. /*2c */
  124. {_CWORD, offsetof(_cmsg, Reason)},
  125. /*2d */
  126. {_CWORD, offsetof(_cmsg, Reason_B3)},
  127. /*2e */
  128. {_CWORD, offsetof(_cmsg, Reject)},
  129. /*2f */
  130. {_CSTRUCT, offsetof(_cmsg, Useruserdata)}
  131. };
  132. static unsigned char *cpars[] =
  133. {
  134. /* ALERT_REQ */ [0x01] = "\x03\x04\x0c\x27\x2f\x1c\x01\x01",
  135. /* 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",
  136. /* DISCONNECT_REQ */ [0x04] = "\x03\x04\x0c\x27\x2f\x1c\x01\x01",
  137. /* LISTEN_REQ */ [0x05] = "\x03\x25\x12\x13\x10\x11\x01",
  138. /* INFO_REQ */ [0x08] = "\x03\x0e\x04\x0c\x27\x2f\x1c\x01\x01",
  139. /* FACILITY_REQ */ [0x09] = "\x03\x1f\x1e\x01",
  140. /* SELECT_B_PROTOCOL_REQ */ [0x0a] = "\x03\x0d\x06\x08\x0a\x05\x07\x09\x01\x01",
  141. /* CONNECT_B3_REQ */ [0x0b] = "\x03\x2b\x01",
  142. /* DISCONNECT_B3_REQ */ [0x0d] = "\x03\x2b\x01",
  143. /* DATA_B3_REQ */ [0x0f] = "\x03\x18\x1a\x19\x20\x01",
  144. /* RESET_B3_REQ */ [0x10] = "\x03\x2b\x01",
  145. /* ALERT_CONF */ [0x13] = "\x03\x23\x01",
  146. /* CONNECT_CONF */ [0x14] = "\x03\x23\x01",
  147. /* DISCONNECT_CONF */ [0x16] = "\x03\x23\x01",
  148. /* LISTEN_CONF */ [0x17] = "\x03\x23\x01",
  149. /* MANUFACTURER_REQ */ [0x18] = "\x03\x2a\x15\x21\x29\x01",
  150. /* INFO_CONF */ [0x1a] = "\x03\x23\x01",
  151. /* FACILITY_CONF */ [0x1b] = "\x03\x23\x1f\x1b\x01",
  152. /* SELECT_B_PROTOCOL_CONF */ [0x1c] = "\x03\x23\x01",
  153. /* CONNECT_B3_CONF */ [0x1d] = "\x03\x23\x01",
  154. /* DISCONNECT_B3_CONF */ [0x1f] = "\x03\x23\x01",
  155. /* DATA_B3_CONF */ [0x21] = "\x03\x19\x23\x01",
  156. /* RESET_B3_CONF */ [0x22] = "\x03\x23\x01",
  157. /* CONNECT_IND */ [0x26] = "\x03\x14\x0e\x10\x0f\x11\x0b\x28\x22\x04\x0c\x27\x2f\x1c\x01\x01",
  158. /* CONNECT_ACTIVE_IND */ [0x27] = "\x03\x16\x17\x28\x01",
  159. /* DISCONNECT_IND */ [0x28] = "\x03\x2c\x01",
  160. /* MANUFACTURER_CONF */ [0x2a] = "\x03\x2a\x15\x21\x29\x01",
  161. /* INFO_IND */ [0x2c] = "\x03\x26\x24\x01",
  162. /* FACILITY_IND */ [0x2d] = "\x03\x1f\x1d\x01",
  163. /* CONNECT_B3_IND */ [0x2f] = "\x03\x2b\x01",
  164. /* CONNECT_B3_ACTIVE_IND */ [0x30] = "\x03\x2b\x01",
  165. /* DISCONNECT_B3_IND */ [0x31] = "\x03\x2d\x2b\x01",
  166. /* DATA_B3_IND */ [0x33] = "\x03\x18\x1a\x19\x20\x01",
  167. /* RESET_B3_IND */ [0x34] = "\x03\x2b\x01",
  168. /* CONNECT_B3_T90_ACTIVE_IND */ [0x35] = "\x03\x2b\x01",
  169. /* CONNECT_RESP */ [0x38] = "\x03\x2e\x0d\x06\x08\x0a\x05\x07\x09\x01\x16\x17\x28\x04\x0c\x27\x2f\x1c\x01\x01",
  170. /* CONNECT_ACTIVE_RESP */ [0x39] = "\x03\x01",
  171. /* DISCONNECT_RESP */ [0x3a] = "\x03\x01",
  172. /* MANUFACTURER_IND */ [0x3c] = "\x03\x2a\x15\x21\x29\x01",
  173. /* INFO_RESP */ [0x3e] = "\x03\x01",
  174. /* FACILITY_RESP */ [0x3f] = "\x03\x1f\x01",
  175. /* CONNECT_B3_RESP */ [0x41] = "\x03\x2e\x2b\x01",
  176. /* CONNECT_B3_ACTIVE_RESP */ [0x42] = "\x03\x01",
  177. /* DISCONNECT_B3_RESP */ [0x43] = "\x03\x01",
  178. /* DATA_B3_RESP */ [0x45] = "\x03\x19\x01",
  179. /* RESET_B3_RESP */ [0x46] = "\x03\x01",
  180. /* CONNECT_B3_T90_ACTIVE_RESP */ [0x47] = "\x03\x01",
  181. /* MANUFACTURER_RESP */ [0x4e] = "\x03\x2a\x15\x21\x29\x01",
  182. };
  183. /*-------------------------------------------------------*/
  184. #define byteTLcpy(x, y) *(u8 *)(x) = *(u8 *)(y);
  185. #define wordTLcpy(x, y) *(u16 *)(x) = *(u16 *)(y);
  186. #define dwordTLcpy(x, y) memcpy(x, y, 4);
  187. #define structTLcpy(x, y, l) memcpy(x, y, l)
  188. #define structTLcpyovl(x, y, l) memmove(x, y, l)
  189. #define byteTRcpy(x, y) *(u8 *)(y) = *(u8 *)(x);
  190. #define wordTRcpy(x, y) *(u16 *)(y) = *(u16 *)(x);
  191. #define dwordTRcpy(x, y) memcpy(y, x, 4);
  192. #define structTRcpy(x, y, l) memcpy(y, x, l)
  193. #define structTRcpyovl(x, y, l) memmove(y, x, l)
  194. /*-------------------------------------------------------*/
  195. static unsigned command_2_index(u8 c, u8 sc)
  196. {
  197. if (c & 0x80)
  198. c = 0x9 + (c & 0x0f);
  199. else if (c == 0x41)
  200. c = 0x9 + 0x1;
  201. if (c > 0x18)
  202. c = 0x00;
  203. return (sc & 3) * (0x9 + 0x9) + c;
  204. }
  205. /**
  206. * capi_cmd2par() - find parameter string for CAPI 2.0 command/subcommand
  207. * @cmd: command number
  208. * @subcmd: subcommand number
  209. *
  210. * Return value: static string, NULL if command/subcommand unknown
  211. */
  212. static unsigned char *capi_cmd2par(u8 cmd, u8 subcmd)
  213. {
  214. return cpars[command_2_index(cmd, subcmd)];
  215. }
  216. /*-------------------------------------------------------*/
  217. #define TYP (cdef[cmsg->par[cmsg->p]].typ)
  218. #define OFF (((u8 *)cmsg) + cdef[cmsg->par[cmsg->p]].off)
  219. static void jumpcstruct(_cmsg *cmsg)
  220. {
  221. unsigned layer;
  222. for (cmsg->p++, layer = 1; layer;) {
  223. /* $$$$$ assert (cmsg->p); */
  224. cmsg->p++;
  225. switch (TYP) {
  226. case _CMSTRUCT:
  227. layer++;
  228. break;
  229. case _CEND:
  230. layer--;
  231. break;
  232. }
  233. }
  234. }
  235. /*-------------------------------------------------------*/
  236. static char *mnames[] =
  237. {
  238. [0x01] = "ALERT_REQ",
  239. [0x02] = "CONNECT_REQ",
  240. [0x04] = "DISCONNECT_REQ",
  241. [0x05] = "LISTEN_REQ",
  242. [0x08] = "INFO_REQ",
  243. [0x09] = "FACILITY_REQ",
  244. [0x0a] = "SELECT_B_PROTOCOL_REQ",
  245. [0x0b] = "CONNECT_B3_REQ",
  246. [0x0d] = "DISCONNECT_B3_REQ",
  247. [0x0f] = "DATA_B3_REQ",
  248. [0x10] = "RESET_B3_REQ",
  249. [0x13] = "ALERT_CONF",
  250. [0x14] = "CONNECT_CONF",
  251. [0x16] = "DISCONNECT_CONF",
  252. [0x17] = "LISTEN_CONF",
  253. [0x18] = "MANUFACTURER_REQ",
  254. [0x1a] = "INFO_CONF",
  255. [0x1b] = "FACILITY_CONF",
  256. [0x1c] = "SELECT_B_PROTOCOL_CONF",
  257. [0x1d] = "CONNECT_B3_CONF",
  258. [0x1f] = "DISCONNECT_B3_CONF",
  259. [0x21] = "DATA_B3_CONF",
  260. [0x22] = "RESET_B3_CONF",
  261. [0x26] = "CONNECT_IND",
  262. [0x27] = "CONNECT_ACTIVE_IND",
  263. [0x28] = "DISCONNECT_IND",
  264. [0x2a] = "MANUFACTURER_CONF",
  265. [0x2c] = "INFO_IND",
  266. [0x2d] = "FACILITY_IND",
  267. [0x2f] = "CONNECT_B3_IND",
  268. [0x30] = "CONNECT_B3_ACTIVE_IND",
  269. [0x31] = "DISCONNECT_B3_IND",
  270. [0x33] = "DATA_B3_IND",
  271. [0x34] = "RESET_B3_IND",
  272. [0x35] = "CONNECT_B3_T90_ACTIVE_IND",
  273. [0x38] = "CONNECT_RESP",
  274. [0x39] = "CONNECT_ACTIVE_RESP",
  275. [0x3a] = "DISCONNECT_RESP",
  276. [0x3c] = "MANUFACTURER_IND",
  277. [0x3e] = "INFO_RESP",
  278. [0x3f] = "FACILITY_RESP",
  279. [0x41] = "CONNECT_B3_RESP",
  280. [0x42] = "CONNECT_B3_ACTIVE_RESP",
  281. [0x43] = "DISCONNECT_B3_RESP",
  282. [0x45] = "DATA_B3_RESP",
  283. [0x46] = "RESET_B3_RESP",
  284. [0x47] = "CONNECT_B3_T90_ACTIVE_RESP",
  285. [0x4e] = "MANUFACTURER_RESP"
  286. };
  287. /**
  288. * capi_cmd2str() - convert CAPI 2.0 command/subcommand number to name
  289. * @cmd: command number
  290. * @subcmd: subcommand number
  291. *
  292. * Return value: static string
  293. */
  294. char *capi_cmd2str(u8 cmd, u8 subcmd)
  295. {
  296. char *result;
  297. result = mnames[command_2_index(cmd, subcmd)];
  298. if (result == NULL)
  299. result = "INVALID_COMMAND";
  300. return result;
  301. }
  302. /*-------------------------------------------------------*/
  303. #ifdef CONFIG_CAPI_TRACE
  304. /*-------------------------------------------------------*/
  305. static char *pnames[] =
  306. {
  307. /*00 */ NULL,
  308. /*01 */ NULL,
  309. /*02 */ NULL,
  310. /*03 */ "Controller/PLCI/NCCI",
  311. /*04 */ "AdditionalInfo",
  312. /*05 */ "B1configuration",
  313. /*06 */ "B1protocol",
  314. /*07 */ "B2configuration",
  315. /*08 */ "B2protocol",
  316. /*09 */ "B3configuration",
  317. /*0a */ "B3protocol",
  318. /*0b */ "BC",
  319. /*0c */ "BChannelinformation",
  320. /*0d */ "BProtocol",
  321. /*0e */ "CalledPartyNumber",
  322. /*0f */ "CalledPartySubaddress",
  323. /*10 */ "CallingPartyNumber",
  324. /*11 */ "CallingPartySubaddress",
  325. /*12 */ "CIPmask",
  326. /*13 */ "CIPmask2",
  327. /*14 */ "CIPValue",
  328. /*15 */ "Class",
  329. /*16 */ "ConnectedNumber",
  330. /*17 */ "ConnectedSubaddress",
  331. /*18 */ "Data32",
  332. /*19 */ "DataHandle",
  333. /*1a */ "DataLength",
  334. /*1b */ "FacilityConfirmationParameter",
  335. /*1c */ "Facilitydataarray",
  336. /*1d */ "FacilityIndicationParameter",
  337. /*1e */ "FacilityRequestParameter",
  338. /*1f */ "FacilitySelector",
  339. /*20 */ "Flags",
  340. /*21 */ "Function",
  341. /*22 */ "HLC",
  342. /*23 */ "Info",
  343. /*24 */ "InfoElement",
  344. /*25 */ "InfoMask",
  345. /*26 */ "InfoNumber",
  346. /*27 */ "Keypadfacility",
  347. /*28 */ "LLC",
  348. /*29 */ "ManuData",
  349. /*2a */ "ManuID",
  350. /*2b */ "NCPI",
  351. /*2c */ "Reason",
  352. /*2d */ "Reason_B3",
  353. /*2e */ "Reject",
  354. /*2f */ "Useruserdata"
  355. };
  356. #include <linux/stdarg.h>
  357. /*-------------------------------------------------------*/
  358. static _cdebbuf *bufprint(_cdebbuf *cdb, char *fmt, ...)
  359. {
  360. va_list f;
  361. size_t n, r;
  362. if (!cdb)
  363. return NULL;
  364. va_start(f, fmt);
  365. r = cdb->size - cdb->pos;
  366. n = vsnprintf(cdb->p, r, fmt, f);
  367. va_end(f);
  368. if (n >= r) {
  369. /* truncated, need bigger buffer */
  370. size_t ns = 2 * cdb->size;
  371. u_char *nb;
  372. while ((ns - cdb->pos) <= n)
  373. ns *= 2;
  374. nb = kmalloc(ns, GFP_ATOMIC);
  375. if (!nb) {
  376. cdebbuf_free(cdb);
  377. return NULL;
  378. }
  379. memcpy(nb, cdb->buf, cdb->pos);
  380. kfree(cdb->buf);
  381. nb[cdb->pos] = 0;
  382. cdb->buf = nb;
  383. cdb->p = cdb->buf + cdb->pos;
  384. cdb->size = ns;
  385. va_start(f, fmt);
  386. r = cdb->size - cdb->pos;
  387. n = vsnprintf(cdb->p, r, fmt, f);
  388. va_end(f);
  389. }
  390. cdb->p += n;
  391. cdb->pos += n;
  392. return cdb;
  393. }
  394. static _cdebbuf *printstructlen(_cdebbuf *cdb, u8 *m, unsigned len)
  395. {
  396. unsigned hex = 0;
  397. if (!cdb)
  398. return NULL;
  399. for (; len; len--, m++)
  400. if (isalnum(*m) || *m == ' ') {
  401. if (hex)
  402. cdb = bufprint(cdb, ">");
  403. cdb = bufprint(cdb, "%c", *m);
  404. hex = 0;
  405. } else {
  406. if (!hex)
  407. cdb = bufprint(cdb, "<%02x", *m);
  408. else
  409. cdb = bufprint(cdb, " %02x", *m);
  410. hex = 1;
  411. }
  412. if (hex)
  413. cdb = bufprint(cdb, ">");
  414. return cdb;
  415. }
  416. static _cdebbuf *printstruct(_cdebbuf *cdb, u8 *m)
  417. {
  418. unsigned len;
  419. if (m[0] != 0xff) {
  420. len = m[0];
  421. m += 1;
  422. } else {
  423. len = ((u16 *) (m + 1))[0];
  424. m += 3;
  425. }
  426. cdb = printstructlen(cdb, m, len);
  427. return cdb;
  428. }
  429. /*-------------------------------------------------------*/
  430. #define NAME (pnames[cmsg->par[cmsg->p]])
  431. static _cdebbuf *protocol_message_2_pars(_cdebbuf *cdb, _cmsg *cmsg, int level)
  432. {
  433. if (!cmsg->par)
  434. return NULL; /* invalid command/subcommand */
  435. for (; TYP != _CEND; cmsg->p++) {
  436. int slen = 29 + 3 - level;
  437. int i;
  438. if (!cdb)
  439. return NULL;
  440. cdb = bufprint(cdb, " ");
  441. for (i = 0; i < level - 1; i++)
  442. cdb = bufprint(cdb, " ");
  443. switch (TYP) {
  444. case _CBYTE:
  445. cdb = bufprint(cdb, "%-*s = 0x%x\n", slen, NAME, *(u8 *) (cmsg->m + cmsg->l));
  446. cmsg->l++;
  447. break;
  448. case _CWORD:
  449. cdb = bufprint(cdb, "%-*s = 0x%x\n", slen, NAME, *(u16 *) (cmsg->m + cmsg->l));
  450. cmsg->l += 2;
  451. break;
  452. case _CDWORD:
  453. cdb = bufprint(cdb, "%-*s = 0x%lx\n", slen, NAME, *(u32 *) (cmsg->m + cmsg->l));
  454. cmsg->l += 4;
  455. break;
  456. case _CSTRUCT:
  457. cdb = bufprint(cdb, "%-*s = ", slen, NAME);
  458. if (cmsg->m[cmsg->l] == '\0')
  459. cdb = bufprint(cdb, "default");
  460. else
  461. cdb = printstruct(cdb, cmsg->m + cmsg->l);
  462. cdb = bufprint(cdb, "\n");
  463. if (cmsg->m[cmsg->l] != 0xff)
  464. cmsg->l += 1 + cmsg->m[cmsg->l];
  465. else
  466. cmsg->l += 3 + *(u16 *) (cmsg->m + cmsg->l + 1);
  467. break;
  468. case _CMSTRUCT:
  469. /*----- Metastruktur 0 -----*/
  470. if (cmsg->m[cmsg->l] == '\0') {
  471. cdb = bufprint(cdb, "%-*s = default\n", slen, NAME);
  472. cmsg->l++;
  473. jumpcstruct(cmsg);
  474. } else {
  475. char *name = NAME;
  476. unsigned _l = cmsg->l;
  477. cdb = bufprint(cdb, "%-*s\n", slen, name);
  478. cmsg->l = (cmsg->m + _l)[0] == 255 ? cmsg->l + 3 : cmsg->l + 1;
  479. cmsg->p++;
  480. cdb = protocol_message_2_pars(cdb, cmsg, level + 1);
  481. }
  482. break;
  483. }
  484. }
  485. return cdb;
  486. }
  487. /*-------------------------------------------------------*/
  488. static _cdebbuf *g_debbuf;
  489. static u_long g_debbuf_lock;
  490. static _cmsg *g_cmsg;
  491. static _cdebbuf *cdebbuf_alloc(void)
  492. {
  493. _cdebbuf *cdb;
  494. if (likely(!test_and_set_bit(1, &g_debbuf_lock))) {
  495. cdb = g_debbuf;
  496. goto init;
  497. } else
  498. cdb = kmalloc(sizeof(_cdebbuf), GFP_ATOMIC);
  499. if (!cdb)
  500. return NULL;
  501. cdb->buf = kmalloc(CDEBUG_SIZE, GFP_ATOMIC);
  502. if (!cdb->buf) {
  503. kfree(cdb);
  504. return NULL;
  505. }
  506. cdb->size = CDEBUG_SIZE;
  507. init:
  508. cdb->buf[0] = 0;
  509. cdb->p = cdb->buf;
  510. cdb->pos = 0;
  511. return cdb;
  512. }
  513. /**
  514. * cdebbuf_free() - free CAPI debug buffer
  515. * @cdb: buffer to free
  516. */
  517. void cdebbuf_free(_cdebbuf *cdb)
  518. {
  519. if (likely(cdb == g_debbuf)) {
  520. test_and_clear_bit(1, &g_debbuf_lock);
  521. return;
  522. }
  523. if (likely(cdb))
  524. kfree(cdb->buf);
  525. kfree(cdb);
  526. }
  527. /**
  528. * capi_message2str() - format CAPI 2.0 message for printing
  529. * @msg: CAPI 2.0 message
  530. *
  531. * Allocates a CAPI debug buffer and fills it with a printable representation
  532. * of the CAPI 2.0 message in @msg.
  533. * Return value: allocated debug buffer, NULL on error
  534. * The returned buffer should be freed by a call to cdebbuf_free() after use.
  535. */
  536. _cdebbuf *capi_message2str(u8 *msg)
  537. {
  538. _cdebbuf *cdb;
  539. _cmsg *cmsg;
  540. cdb = cdebbuf_alloc();
  541. if (unlikely(!cdb))
  542. return NULL;
  543. if (likely(cdb == g_debbuf))
  544. cmsg = g_cmsg;
  545. else
  546. cmsg = kmalloc(sizeof(_cmsg), GFP_ATOMIC);
  547. if (unlikely(!cmsg)) {
  548. cdebbuf_free(cdb);
  549. return NULL;
  550. }
  551. cmsg->m = msg;
  552. cmsg->l = 8;
  553. cmsg->p = 0;
  554. byteTRcpy(cmsg->m + 4, &cmsg->Command);
  555. byteTRcpy(cmsg->m + 5, &cmsg->Subcommand);
  556. cmsg->par = capi_cmd2par(cmsg->Command, cmsg->Subcommand);
  557. cdb = bufprint(cdb, "%-26s ID=%03d #0x%04x LEN=%04d\n",
  558. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  559. ((unsigned short *) msg)[1],
  560. ((unsigned short *) msg)[3],
  561. ((unsigned short *) msg)[0]);
  562. cdb = protocol_message_2_pars(cdb, cmsg, 1);
  563. if (unlikely(cmsg != g_cmsg))
  564. kfree(cmsg);
  565. return cdb;
  566. }
  567. int __init cdebug_init(void)
  568. {
  569. g_cmsg = kmalloc(sizeof(_cmsg), GFP_KERNEL);
  570. if (!g_cmsg)
  571. return -ENOMEM;
  572. g_debbuf = kmalloc(sizeof(_cdebbuf), GFP_KERNEL);
  573. if (!g_debbuf) {
  574. kfree(g_cmsg);
  575. return -ENOMEM;
  576. }
  577. g_debbuf->buf = kmalloc(CDEBUG_GSIZE, GFP_KERNEL);
  578. if (!g_debbuf->buf) {
  579. kfree(g_cmsg);
  580. kfree(g_debbuf);
  581. return -ENOMEM;
  582. }
  583. g_debbuf->size = CDEBUG_GSIZE;
  584. g_debbuf->buf[0] = 0;
  585. g_debbuf->p = g_debbuf->buf;
  586. g_debbuf->pos = 0;
  587. return 0;
  588. }
  589. void cdebug_exit(void)
  590. {
  591. if (g_debbuf)
  592. kfree(g_debbuf->buf);
  593. kfree(g_debbuf);
  594. kfree(g_cmsg);
  595. }
  596. #else /* !CONFIG_CAPI_TRACE */
  597. static _cdebbuf g_debbuf = {"CONFIG_CAPI_TRACE not enabled", NULL, 0, 0};
  598. _cdebbuf *capi_message2str(u8 *msg)
  599. {
  600. return &g_debbuf;
  601. }
  602. _cdebbuf *capi_cmsg2str(_cmsg *cmsg)
  603. {
  604. return &g_debbuf;
  605. }
  606. void cdebbuf_free(_cdebbuf *cdb)
  607. {
  608. }
  609. int __init cdebug_init(void)
  610. {
  611. return 0;
  612. }
  613. void cdebug_exit(void)
  614. {
  615. }
  616. #endif