capiutil.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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 <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. #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. /* from CAPI2.0 DDK AVM Berlin GmbH */
  22. typedef struct {
  23. int typ;
  24. size_t off;
  25. } _cdef;
  26. #define _CBYTE 1
  27. #define _CWORD 2
  28. #define _CDWORD 3
  29. #define _CSTRUCT 4
  30. #define _CMSTRUCT 5
  31. #define _CEND 6
  32. static _cdef cdef[] =
  33. {
  34. /*00 */
  35. {_CEND},
  36. /*01 */
  37. {_CEND},
  38. /*02 */
  39. {_CEND},
  40. /*03 */
  41. {_CDWORD, offsetof(_cmsg, adr.adrController)},
  42. /*04 */
  43. {_CMSTRUCT, offsetof(_cmsg, AdditionalInfo)},
  44. /*05 */
  45. {_CSTRUCT, offsetof(_cmsg, B1configuration)},
  46. /*06 */
  47. {_CWORD, offsetof(_cmsg, B1protocol)},
  48. /*07 */
  49. {_CSTRUCT, offsetof(_cmsg, B2configuration)},
  50. /*08 */
  51. {_CWORD, offsetof(_cmsg, B2protocol)},
  52. /*09 */
  53. {_CSTRUCT, offsetof(_cmsg, B3configuration)},
  54. /*0a */
  55. {_CWORD, offsetof(_cmsg, B3protocol)},
  56. /*0b */
  57. {_CSTRUCT, offsetof(_cmsg, BC)},
  58. /*0c */
  59. {_CSTRUCT, offsetof(_cmsg, BChannelinformation)},
  60. /*0d */
  61. {_CMSTRUCT, offsetof(_cmsg, BProtocol)},
  62. /*0e */
  63. {_CSTRUCT, offsetof(_cmsg, CalledPartyNumber)},
  64. /*0f */
  65. {_CSTRUCT, offsetof(_cmsg, CalledPartySubaddress)},
  66. /*10 */
  67. {_CSTRUCT, offsetof(_cmsg, CallingPartyNumber)},
  68. /*11 */
  69. {_CSTRUCT, offsetof(_cmsg, CallingPartySubaddress)},
  70. /*12 */
  71. {_CDWORD, offsetof(_cmsg, CIPmask)},
  72. /*13 */
  73. {_CDWORD, offsetof(_cmsg, CIPmask2)},
  74. /*14 */
  75. {_CWORD, offsetof(_cmsg, CIPValue)},
  76. /*15 */
  77. {_CDWORD, offsetof(_cmsg, Class)},
  78. /*16 */
  79. {_CSTRUCT, offsetof(_cmsg, ConnectedNumber)},
  80. /*17 */
  81. {_CSTRUCT, offsetof(_cmsg, ConnectedSubaddress)},
  82. /*18 */
  83. {_CDWORD, offsetof(_cmsg, Data)},
  84. /*19 */
  85. {_CWORD, offsetof(_cmsg, DataHandle)},
  86. /*1a */
  87. {_CWORD, offsetof(_cmsg, DataLength)},
  88. /*1b */
  89. {_CSTRUCT, offsetof(_cmsg, FacilityConfirmationParameter)},
  90. /*1c */
  91. {_CSTRUCT, offsetof(_cmsg, Facilitydataarray)},
  92. /*1d */
  93. {_CSTRUCT, offsetof(_cmsg, FacilityIndicationParameter)},
  94. /*1e */
  95. {_CSTRUCT, offsetof(_cmsg, FacilityRequestParameter)},
  96. /*1f */
  97. {_CWORD, offsetof(_cmsg, FacilitySelector)},
  98. /*20 */
  99. {_CWORD, offsetof(_cmsg, Flags)},
  100. /*21 */
  101. {_CDWORD, offsetof(_cmsg, Function)},
  102. /*22 */
  103. {_CSTRUCT, offsetof(_cmsg, HLC)},
  104. /*23 */
  105. {_CWORD, offsetof(_cmsg, Info)},
  106. /*24 */
  107. {_CSTRUCT, offsetof(_cmsg, InfoElement)},
  108. /*25 */
  109. {_CDWORD, offsetof(_cmsg, InfoMask)},
  110. /*26 */
  111. {_CWORD, offsetof(_cmsg, InfoNumber)},
  112. /*27 */
  113. {_CSTRUCT, offsetof(_cmsg, Keypadfacility)},
  114. /*28 */
  115. {_CSTRUCT, offsetof(_cmsg, LLC)},
  116. /*29 */
  117. {_CSTRUCT, offsetof(_cmsg, ManuData)},
  118. /*2a */
  119. {_CDWORD, offsetof(_cmsg, ManuID)},
  120. /*2b */
  121. {_CSTRUCT, offsetof(_cmsg, NCPI)},
  122. /*2c */
  123. {_CWORD, offsetof(_cmsg, Reason)},
  124. /*2d */
  125. {_CWORD, offsetof(_cmsg, Reason_B3)},
  126. /*2e */
  127. {_CWORD, offsetof(_cmsg, Reject)},
  128. /*2f */
  129. {_CSTRUCT, offsetof(_cmsg, Useruserdata)}
  130. };
  131. static unsigned char *cpars[] =
  132. {
  133. /* ALERT_REQ */ [0x01] = "\x03\x04\x0c\x27\x2f\x1c\x01\x01",
  134. /* 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",
  135. /* DISCONNECT_REQ */ [0x04] = "\x03\x04\x0c\x27\x2f\x1c\x01\x01",
  136. /* LISTEN_REQ */ [0x05] = "\x03\x25\x12\x13\x10\x11\x01",
  137. /* INFO_REQ */ [0x08] = "\x03\x0e\x04\x0c\x27\x2f\x1c\x01\x01",
  138. /* FACILITY_REQ */ [0x09] = "\x03\x1f\x1e\x01",
  139. /* SELECT_B_PROTOCOL_REQ */ [0x0a] = "\x03\x0d\x06\x08\x0a\x05\x07\x09\x01\x01",
  140. /* CONNECT_B3_REQ */ [0x0b] = "\x03\x2b\x01",
  141. /* DISCONNECT_B3_REQ */ [0x0d] = "\x03\x2b\x01",
  142. /* DATA_B3_REQ */ [0x0f] = "\x03\x18\x1a\x19\x20\x01",
  143. /* RESET_B3_REQ */ [0x10] = "\x03\x2b\x01",
  144. /* ALERT_CONF */ [0x13] = "\x03\x23\x01",
  145. /* CONNECT_CONF */ [0x14] = "\x03\x23\x01",
  146. /* DISCONNECT_CONF */ [0x16] = "\x03\x23\x01",
  147. /* LISTEN_CONF */ [0x17] = "\x03\x23\x01",
  148. /* MANUFACTURER_REQ */ [0x18] = "\x03\x2a\x15\x21\x29\x01",
  149. /* INFO_CONF */ [0x1a] = "\x03\x23\x01",
  150. /* FACILITY_CONF */ [0x1b] = "\x03\x23\x1f\x1b\x01",
  151. /* SELECT_B_PROTOCOL_CONF */ [0x1c] = "\x03\x23\x01",
  152. /* CONNECT_B3_CONF */ [0x1d] = "\x03\x23\x01",
  153. /* DISCONNECT_B3_CONF */ [0x1f] = "\x03\x23\x01",
  154. /* DATA_B3_CONF */ [0x21] = "\x03\x19\x23\x01",
  155. /* RESET_B3_CONF */ [0x22] = "\x03\x23\x01",
  156. /* CONNECT_IND */ [0x26] = "\x03\x14\x0e\x10\x0f\x11\x0b\x28\x22\x04\x0c\x27\x2f\x1c\x01\x01",
  157. /* CONNECT_ACTIVE_IND */ [0x27] = "\x03\x16\x17\x28\x01",
  158. /* DISCONNECT_IND */ [0x28] = "\x03\x2c\x01",
  159. /* MANUFACTURER_CONF */ [0x2a] = "\x03\x2a\x15\x21\x29\x01",
  160. /* INFO_IND */ [0x2c] = "\x03\x26\x24\x01",
  161. /* FACILITY_IND */ [0x2d] = "\x03\x1f\x1d\x01",
  162. /* CONNECT_B3_IND */ [0x2f] = "\x03\x2b\x01",
  163. /* CONNECT_B3_ACTIVE_IND */ [0x30] = "\x03\x2b\x01",
  164. /* DISCONNECT_B3_IND */ [0x31] = "\x03\x2d\x2b\x01",
  165. /* DATA_B3_IND */ [0x33] = "\x03\x18\x1a\x19\x20\x01",
  166. /* RESET_B3_IND */ [0x34] = "\x03\x2b\x01",
  167. /* CONNECT_B3_T90_ACTIVE_IND */ [0x35] = "\x03\x2b\x01",
  168. /* CONNECT_RESP */ [0x38] = "\x03\x2e\x0d\x06\x08\x0a\x05\x07\x09\x01\x16\x17\x28\x04\x0c\x27\x2f\x1c\x01\x01",
  169. /* CONNECT_ACTIVE_RESP */ [0x39] = "\x03\x01",
  170. /* DISCONNECT_RESP */ [0x3a] = "\x03\x01",
  171. /* MANUFACTURER_IND */ [0x3c] = "\x03\x2a\x15\x21\x29\x01",
  172. /* INFO_RESP */ [0x3e] = "\x03\x01",
  173. /* FACILITY_RESP */ [0x3f] = "\x03\x1f\x01",
  174. /* CONNECT_B3_RESP */ [0x41] = "\x03\x2e\x2b\x01",
  175. /* CONNECT_B3_ACTIVE_RESP */ [0x42] = "\x03\x01",
  176. /* DISCONNECT_B3_RESP */ [0x43] = "\x03\x01",
  177. /* DATA_B3_RESP */ [0x45] = "\x03\x19\x01",
  178. /* RESET_B3_RESP */ [0x46] = "\x03\x01",
  179. /* CONNECT_B3_T90_ACTIVE_RESP */ [0x47] = "\x03\x01",
  180. /* MANUFACTURER_RESP */ [0x4e] = "\x03\x2a\x15\x21\x29\x01",
  181. };
  182. /*-------------------------------------------------------*/
  183. #define byteTLcpy(x, y) *(u8 *)(x) = *(u8 *)(y);
  184. #define wordTLcpy(x, y) *(u16 *)(x) = *(u16 *)(y);
  185. #define dwordTLcpy(x, y) memcpy(x, y, 4);
  186. #define structTLcpy(x, y, l) memcpy(x, y, l)
  187. #define structTLcpyovl(x, y, l) memmove(x, y, l)
  188. #define byteTRcpy(x, y) *(u8 *)(y) = *(u8 *)(x);
  189. #define wordTRcpy(x, y) *(u16 *)(y) = *(u16 *)(x);
  190. #define dwordTRcpy(x, y) memcpy(y, x, 4);
  191. #define structTRcpy(x, y, l) memcpy(y, x, l)
  192. #define structTRcpyovl(x, y, l) memmove(y, x, l)
  193. /*-------------------------------------------------------*/
  194. static unsigned command_2_index(u8 c, u8 sc)
  195. {
  196. if (c & 0x80)
  197. c = 0x9 + (c & 0x0f);
  198. else if (c == 0x41)
  199. c = 0x9 + 0x1;
  200. if (c > 0x18)
  201. c = 0x00;
  202. return (sc & 3) * (0x9 + 0x9) + c;
  203. }
  204. /**
  205. * capi_cmd2par() - find parameter string for CAPI 2.0 command/subcommand
  206. * @cmd: command number
  207. * @subcmd: subcommand number
  208. *
  209. * Return value: static string, NULL if command/subcommand unknown
  210. */
  211. static unsigned char *capi_cmd2par(u8 cmd, u8 subcmd)
  212. {
  213. return cpars[command_2_index(cmd, subcmd)];
  214. }
  215. /*-------------------------------------------------------*/
  216. #define TYP (cdef[cmsg->par[cmsg->p]].typ)
  217. #define OFF (((u8 *)cmsg) + cdef[cmsg->par[cmsg->p]].off)
  218. static void jumpcstruct(_cmsg *cmsg)
  219. {
  220. unsigned layer;
  221. for (cmsg->p++, layer = 1; layer;) {
  222. /* $$$$$ assert (cmsg->p); */
  223. cmsg->p++;
  224. switch (TYP) {
  225. case _CMSTRUCT:
  226. layer++;
  227. break;
  228. case _CEND:
  229. layer--;
  230. break;
  231. }
  232. }
  233. }
  234. /*-------------------------------------------------------*/
  235. static void pars_2_message(_cmsg *cmsg)
  236. {
  237. for (; TYP != _CEND; cmsg->p++) {
  238. switch (TYP) {
  239. case _CBYTE:
  240. byteTLcpy(cmsg->m + cmsg->l, OFF);
  241. cmsg->l++;
  242. break;
  243. case _CWORD:
  244. wordTLcpy(cmsg->m + cmsg->l, OFF);
  245. cmsg->l += 2;
  246. break;
  247. case _CDWORD:
  248. dwordTLcpy(cmsg->m + cmsg->l, OFF);
  249. cmsg->l += 4;
  250. break;
  251. case _CSTRUCT:
  252. if (*(u8 **) OFF == NULL) {
  253. *(cmsg->m + cmsg->l) = '\0';
  254. cmsg->l++;
  255. } else if (**(_cstruct *) OFF != 0xff) {
  256. structTLcpy(cmsg->m + cmsg->l, *(_cstruct *) OFF, 1 + **(_cstruct *) OFF);
  257. cmsg->l += 1 + **(_cstruct *) OFF;
  258. } else {
  259. _cstruct s = *(_cstruct *) OFF;
  260. structTLcpy(cmsg->m + cmsg->l, s, 3 + *(u16 *) (s + 1));
  261. cmsg->l += 3 + *(u16 *) (s + 1);
  262. }
  263. break;
  264. case _CMSTRUCT:
  265. /*----- Metastruktur 0 -----*/
  266. if (*(_cmstruct *) OFF == CAPI_DEFAULT) {
  267. *(cmsg->m + cmsg->l) = '\0';
  268. cmsg->l++;
  269. jumpcstruct(cmsg);
  270. }
  271. /*----- Metastruktur wird composed -----*/
  272. else {
  273. unsigned _l = cmsg->l;
  274. unsigned _ls;
  275. cmsg->l++;
  276. cmsg->p++;
  277. pars_2_message(cmsg);
  278. _ls = cmsg->l - _l - 1;
  279. if (_ls < 255)
  280. (cmsg->m + _l)[0] = (u8) _ls;
  281. else {
  282. structTLcpyovl(cmsg->m + _l + 3, cmsg->m + _l + 1, _ls);
  283. (cmsg->m + _l)[0] = 0xff;
  284. wordTLcpy(cmsg->m + _l + 1, &_ls);
  285. }
  286. }
  287. break;
  288. }
  289. }
  290. }
  291. /**
  292. * capi_cmsg2message() - assemble CAPI 2.0 message from _cmsg structure
  293. * @cmsg: _cmsg structure
  294. * @msg: buffer for assembled message
  295. *
  296. * Return value: 0 for success
  297. */
  298. unsigned capi_cmsg2message(_cmsg *cmsg, u8 *msg)
  299. {
  300. cmsg->m = msg;
  301. cmsg->l = 8;
  302. cmsg->p = 0;
  303. cmsg->par = capi_cmd2par(cmsg->Command, cmsg->Subcommand);
  304. if (!cmsg->par)
  305. return 1; /* invalid command/subcommand */
  306. pars_2_message(cmsg);
  307. wordTLcpy(msg + 0, &cmsg->l);
  308. byteTLcpy(cmsg->m + 4, &cmsg->Command);
  309. byteTLcpy(cmsg->m + 5, &cmsg->Subcommand);
  310. wordTLcpy(cmsg->m + 2, &cmsg->ApplId);
  311. wordTLcpy(cmsg->m + 6, &cmsg->Messagenumber);
  312. return 0;
  313. }
  314. /*-------------------------------------------------------*/
  315. static void message_2_pars(_cmsg *cmsg)
  316. {
  317. for (; TYP != _CEND; cmsg->p++) {
  318. switch (TYP) {
  319. case _CBYTE:
  320. byteTRcpy(cmsg->m + cmsg->l, OFF);
  321. cmsg->l++;
  322. break;
  323. case _CWORD:
  324. wordTRcpy(cmsg->m + cmsg->l, OFF);
  325. cmsg->l += 2;
  326. break;
  327. case _CDWORD:
  328. dwordTRcpy(cmsg->m + cmsg->l, OFF);
  329. cmsg->l += 4;
  330. break;
  331. case _CSTRUCT:
  332. *(u8 **) OFF = cmsg->m + cmsg->l;
  333. if (cmsg->m[cmsg->l] != 0xff)
  334. cmsg->l += 1 + cmsg->m[cmsg->l];
  335. else
  336. cmsg->l += 3 + *(u16 *) (cmsg->m + cmsg->l + 1);
  337. break;
  338. case _CMSTRUCT:
  339. /*----- Metastruktur 0 -----*/
  340. if (cmsg->m[cmsg->l] == '\0') {
  341. *(_cmstruct *) OFF = CAPI_DEFAULT;
  342. cmsg->l++;
  343. jumpcstruct(cmsg);
  344. } else {
  345. unsigned _l = cmsg->l;
  346. *(_cmstruct *) OFF = CAPI_COMPOSE;
  347. cmsg->l = (cmsg->m + _l)[0] == 255 ? cmsg->l + 3 : cmsg->l + 1;
  348. cmsg->p++;
  349. message_2_pars(cmsg);
  350. }
  351. break;
  352. }
  353. }
  354. }
  355. /**
  356. * capi_message2cmsg() - disassemble CAPI 2.0 message into _cmsg structure
  357. * @cmsg: _cmsg structure
  358. * @msg: buffer for assembled message
  359. *
  360. * Return value: 0 for success
  361. */
  362. unsigned capi_message2cmsg(_cmsg *cmsg, u8 *msg)
  363. {
  364. memset(cmsg, 0, sizeof(_cmsg));
  365. cmsg->m = msg;
  366. cmsg->l = 8;
  367. cmsg->p = 0;
  368. byteTRcpy(cmsg->m + 4, &cmsg->Command);
  369. byteTRcpy(cmsg->m + 5, &cmsg->Subcommand);
  370. cmsg->par = capi_cmd2par(cmsg->Command, cmsg->Subcommand);
  371. if (!cmsg->par)
  372. return 1; /* invalid command/subcommand */
  373. message_2_pars(cmsg);
  374. wordTRcpy(msg + 0, &cmsg->l);
  375. wordTRcpy(cmsg->m + 2, &cmsg->ApplId);
  376. wordTRcpy(cmsg->m + 6, &cmsg->Messagenumber);
  377. return 0;
  378. }
  379. /**
  380. * capi_cmsg_header() - initialize header part of _cmsg structure
  381. * @cmsg: _cmsg structure
  382. * @_ApplId: ApplID field value
  383. * @_Command: Command field value
  384. * @_Subcommand: Subcommand field value
  385. * @_Messagenumber: Message Number field value
  386. * @_Controller: Controller/PLCI/NCCI field value
  387. *
  388. * Return value: 0 for success
  389. */
  390. unsigned capi_cmsg_header(_cmsg *cmsg, u16 _ApplId,
  391. u8 _Command, u8 _Subcommand,
  392. u16 _Messagenumber, u32 _Controller)
  393. {
  394. memset(cmsg, 0, sizeof(_cmsg));
  395. cmsg->ApplId = _ApplId;
  396. cmsg->Command = _Command;
  397. cmsg->Subcommand = _Subcommand;
  398. cmsg->Messagenumber = _Messagenumber;
  399. cmsg->adr.adrController = _Controller;
  400. return 0;
  401. }
  402. /*-------------------------------------------------------*/
  403. static char *mnames[] =
  404. {
  405. [0x01] = "ALERT_REQ",
  406. [0x02] = "CONNECT_REQ",
  407. [0x04] = "DISCONNECT_REQ",
  408. [0x05] = "LISTEN_REQ",
  409. [0x08] = "INFO_REQ",
  410. [0x09] = "FACILITY_REQ",
  411. [0x0a] = "SELECT_B_PROTOCOL_REQ",
  412. [0x0b] = "CONNECT_B3_REQ",
  413. [0x0d] = "DISCONNECT_B3_REQ",
  414. [0x0f] = "DATA_B3_REQ",
  415. [0x10] = "RESET_B3_REQ",
  416. [0x13] = "ALERT_CONF",
  417. [0x14] = "CONNECT_CONF",
  418. [0x16] = "DISCONNECT_CONF",
  419. [0x17] = "LISTEN_CONF",
  420. [0x18] = "MANUFACTURER_REQ",
  421. [0x1a] = "INFO_CONF",
  422. [0x1b] = "FACILITY_CONF",
  423. [0x1c] = "SELECT_B_PROTOCOL_CONF",
  424. [0x1d] = "CONNECT_B3_CONF",
  425. [0x1f] = "DISCONNECT_B3_CONF",
  426. [0x21] = "DATA_B3_CONF",
  427. [0x22] = "RESET_B3_CONF",
  428. [0x26] = "CONNECT_IND",
  429. [0x27] = "CONNECT_ACTIVE_IND",
  430. [0x28] = "DISCONNECT_IND",
  431. [0x2a] = "MANUFACTURER_CONF",
  432. [0x2c] = "INFO_IND",
  433. [0x2d] = "FACILITY_IND",
  434. [0x2f] = "CONNECT_B3_IND",
  435. [0x30] = "CONNECT_B3_ACTIVE_IND",
  436. [0x31] = "DISCONNECT_B3_IND",
  437. [0x33] = "DATA_B3_IND",
  438. [0x34] = "RESET_B3_IND",
  439. [0x35] = "CONNECT_B3_T90_ACTIVE_IND",
  440. [0x38] = "CONNECT_RESP",
  441. [0x39] = "CONNECT_ACTIVE_RESP",
  442. [0x3a] = "DISCONNECT_RESP",
  443. [0x3c] = "MANUFACTURER_IND",
  444. [0x3e] = "INFO_RESP",
  445. [0x3f] = "FACILITY_RESP",
  446. [0x41] = "CONNECT_B3_RESP",
  447. [0x42] = "CONNECT_B3_ACTIVE_RESP",
  448. [0x43] = "DISCONNECT_B3_RESP",
  449. [0x45] = "DATA_B3_RESP",
  450. [0x46] = "RESET_B3_RESP",
  451. [0x47] = "CONNECT_B3_T90_ACTIVE_RESP",
  452. [0x4e] = "MANUFACTURER_RESP"
  453. };
  454. /**
  455. * capi_cmd2str() - convert CAPI 2.0 command/subcommand number to name
  456. * @cmd: command number
  457. * @subcmd: subcommand number
  458. *
  459. * Return value: static string
  460. */
  461. char *capi_cmd2str(u8 cmd, u8 subcmd)
  462. {
  463. char *result;
  464. result = mnames[command_2_index(cmd, subcmd)];
  465. if (result == NULL)
  466. result = "INVALID_COMMAND";
  467. return result;
  468. }
  469. /*-------------------------------------------------------*/
  470. #ifdef CONFIG_CAPI_TRACE
  471. /*-------------------------------------------------------*/
  472. static char *pnames[] =
  473. {
  474. /*00 */ NULL,
  475. /*01 */ NULL,
  476. /*02 */ NULL,
  477. /*03 */ "Controller/PLCI/NCCI",
  478. /*04 */ "AdditionalInfo",
  479. /*05 */ "B1configuration",
  480. /*06 */ "B1protocol",
  481. /*07 */ "B2configuration",
  482. /*08 */ "B2protocol",
  483. /*09 */ "B3configuration",
  484. /*0a */ "B3protocol",
  485. /*0b */ "BC",
  486. /*0c */ "BChannelinformation",
  487. /*0d */ "BProtocol",
  488. /*0e */ "CalledPartyNumber",
  489. /*0f */ "CalledPartySubaddress",
  490. /*10 */ "CallingPartyNumber",
  491. /*11 */ "CallingPartySubaddress",
  492. /*12 */ "CIPmask",
  493. /*13 */ "CIPmask2",
  494. /*14 */ "CIPValue",
  495. /*15 */ "Class",
  496. /*16 */ "ConnectedNumber",
  497. /*17 */ "ConnectedSubaddress",
  498. /*18 */ "Data32",
  499. /*19 */ "DataHandle",
  500. /*1a */ "DataLength",
  501. /*1b */ "FacilityConfirmationParameter",
  502. /*1c */ "Facilitydataarray",
  503. /*1d */ "FacilityIndicationParameter",
  504. /*1e */ "FacilityRequestParameter",
  505. /*1f */ "FacilitySelector",
  506. /*20 */ "Flags",
  507. /*21 */ "Function",
  508. /*22 */ "HLC",
  509. /*23 */ "Info",
  510. /*24 */ "InfoElement",
  511. /*25 */ "InfoMask",
  512. /*26 */ "InfoNumber",
  513. /*27 */ "Keypadfacility",
  514. /*28 */ "LLC",
  515. /*29 */ "ManuData",
  516. /*2a */ "ManuID",
  517. /*2b */ "NCPI",
  518. /*2c */ "Reason",
  519. /*2d */ "Reason_B3",
  520. /*2e */ "Reject",
  521. /*2f */ "Useruserdata"
  522. };
  523. #include <stdarg.h>
  524. /*-------------------------------------------------------*/
  525. static _cdebbuf *bufprint(_cdebbuf *cdb, char *fmt, ...)
  526. {
  527. va_list f;
  528. size_t n, r;
  529. if (!cdb)
  530. return NULL;
  531. va_start(f, fmt);
  532. r = cdb->size - cdb->pos;
  533. n = vsnprintf(cdb->p, r, fmt, f);
  534. va_end(f);
  535. if (n >= r) {
  536. /* truncated, need bigger buffer */
  537. size_t ns = 2 * cdb->size;
  538. u_char *nb;
  539. while ((ns - cdb->pos) <= n)
  540. ns *= 2;
  541. nb = kmalloc(ns, GFP_ATOMIC);
  542. if (!nb) {
  543. cdebbuf_free(cdb);
  544. return NULL;
  545. }
  546. memcpy(nb, cdb->buf, cdb->pos);
  547. kfree(cdb->buf);
  548. nb[cdb->pos] = 0;
  549. cdb->buf = nb;
  550. cdb->p = cdb->buf + cdb->pos;
  551. cdb->size = ns;
  552. va_start(f, fmt);
  553. r = cdb->size - cdb->pos;
  554. n = vsnprintf(cdb->p, r, fmt, f);
  555. va_end(f);
  556. }
  557. cdb->p += n;
  558. cdb->pos += n;
  559. return cdb;
  560. }
  561. static _cdebbuf *printstructlen(_cdebbuf *cdb, u8 *m, unsigned len)
  562. {
  563. unsigned hex = 0;
  564. if (!cdb)
  565. return NULL;
  566. for (; len; len--, m++)
  567. if (isalnum(*m) || *m == ' ') {
  568. if (hex)
  569. cdb = bufprint(cdb, ">");
  570. cdb = bufprint(cdb, "%c", *m);
  571. hex = 0;
  572. } else {
  573. if (!hex)
  574. cdb = bufprint(cdb, "<%02x", *m);
  575. else
  576. cdb = bufprint(cdb, " %02x", *m);
  577. hex = 1;
  578. }
  579. if (hex)
  580. cdb = bufprint(cdb, ">");
  581. return cdb;
  582. }
  583. static _cdebbuf *printstruct(_cdebbuf *cdb, u8 *m)
  584. {
  585. unsigned len;
  586. if (m[0] != 0xff) {
  587. len = m[0];
  588. m += 1;
  589. } else {
  590. len = ((u16 *) (m + 1))[0];
  591. m += 3;
  592. }
  593. cdb = printstructlen(cdb, m, len);
  594. return cdb;
  595. }
  596. /*-------------------------------------------------------*/
  597. #define NAME (pnames[cmsg->par[cmsg->p]])
  598. static _cdebbuf *protocol_message_2_pars(_cdebbuf *cdb, _cmsg *cmsg, int level)
  599. {
  600. if (!cmsg->par)
  601. return NULL; /* invalid command/subcommand */
  602. for (; TYP != _CEND; cmsg->p++) {
  603. int slen = 29 + 3 - level;
  604. int i;
  605. if (!cdb)
  606. return NULL;
  607. cdb = bufprint(cdb, " ");
  608. for (i = 0; i < level - 1; i++)
  609. cdb = bufprint(cdb, " ");
  610. switch (TYP) {
  611. case _CBYTE:
  612. cdb = bufprint(cdb, "%-*s = 0x%x\n", slen, NAME, *(u8 *) (cmsg->m + cmsg->l));
  613. cmsg->l++;
  614. break;
  615. case _CWORD:
  616. cdb = bufprint(cdb, "%-*s = 0x%x\n", slen, NAME, *(u16 *) (cmsg->m + cmsg->l));
  617. cmsg->l += 2;
  618. break;
  619. case _CDWORD:
  620. cdb = bufprint(cdb, "%-*s = 0x%lx\n", slen, NAME, *(u32 *) (cmsg->m + cmsg->l));
  621. cmsg->l += 4;
  622. break;
  623. case _CSTRUCT:
  624. cdb = bufprint(cdb, "%-*s = ", slen, NAME);
  625. if (cmsg->m[cmsg->l] == '\0')
  626. cdb = bufprint(cdb, "default");
  627. else
  628. cdb = printstruct(cdb, cmsg->m + cmsg->l);
  629. cdb = bufprint(cdb, "\n");
  630. if (cmsg->m[cmsg->l] != 0xff)
  631. cmsg->l += 1 + cmsg->m[cmsg->l];
  632. else
  633. cmsg->l += 3 + *(u16 *) (cmsg->m + cmsg->l + 1);
  634. break;
  635. case _CMSTRUCT:
  636. /*----- Metastruktur 0 -----*/
  637. if (cmsg->m[cmsg->l] == '\0') {
  638. cdb = bufprint(cdb, "%-*s = default\n", slen, NAME);
  639. cmsg->l++;
  640. jumpcstruct(cmsg);
  641. } else {
  642. char *name = NAME;
  643. unsigned _l = cmsg->l;
  644. cdb = bufprint(cdb, "%-*s\n", slen, name);
  645. cmsg->l = (cmsg->m + _l)[0] == 255 ? cmsg->l + 3 : cmsg->l + 1;
  646. cmsg->p++;
  647. cdb = protocol_message_2_pars(cdb, cmsg, level + 1);
  648. }
  649. break;
  650. }
  651. }
  652. return cdb;
  653. }
  654. /*-------------------------------------------------------*/
  655. static _cdebbuf *g_debbuf;
  656. static u_long g_debbuf_lock;
  657. static _cmsg *g_cmsg;
  658. static _cdebbuf *cdebbuf_alloc(void)
  659. {
  660. _cdebbuf *cdb;
  661. if (likely(!test_and_set_bit(1, &g_debbuf_lock))) {
  662. cdb = g_debbuf;
  663. goto init;
  664. } else
  665. cdb = kmalloc(sizeof(_cdebbuf), GFP_ATOMIC);
  666. if (!cdb)
  667. return NULL;
  668. cdb->buf = kmalloc(CDEBUG_SIZE, GFP_ATOMIC);
  669. if (!cdb->buf) {
  670. kfree(cdb);
  671. return NULL;
  672. }
  673. cdb->size = CDEBUG_SIZE;
  674. init:
  675. cdb->buf[0] = 0;
  676. cdb->p = cdb->buf;
  677. cdb->pos = 0;
  678. return cdb;
  679. }
  680. /**
  681. * cdebbuf_free() - free CAPI debug buffer
  682. * @cdb: buffer to free
  683. */
  684. void cdebbuf_free(_cdebbuf *cdb)
  685. {
  686. if (likely(cdb == g_debbuf)) {
  687. test_and_clear_bit(1, &g_debbuf_lock);
  688. return;
  689. }
  690. if (likely(cdb))
  691. kfree(cdb->buf);
  692. kfree(cdb);
  693. }
  694. /**
  695. * capi_message2str() - format CAPI 2.0 message for printing
  696. * @msg: CAPI 2.0 message
  697. *
  698. * Allocates a CAPI debug buffer and fills it with a printable representation
  699. * of the CAPI 2.0 message in @msg.
  700. * Return value: allocated debug buffer, NULL on error
  701. * The returned buffer should be freed by a call to cdebbuf_free() after use.
  702. */
  703. _cdebbuf *capi_message2str(u8 *msg)
  704. {
  705. _cdebbuf *cdb;
  706. _cmsg *cmsg;
  707. cdb = cdebbuf_alloc();
  708. if (unlikely(!cdb))
  709. return NULL;
  710. if (likely(cdb == g_debbuf))
  711. cmsg = g_cmsg;
  712. else
  713. cmsg = kmalloc(sizeof(_cmsg), GFP_ATOMIC);
  714. if (unlikely(!cmsg)) {
  715. cdebbuf_free(cdb);
  716. return NULL;
  717. }
  718. cmsg->m = msg;
  719. cmsg->l = 8;
  720. cmsg->p = 0;
  721. byteTRcpy(cmsg->m + 4, &cmsg->Command);
  722. byteTRcpy(cmsg->m + 5, &cmsg->Subcommand);
  723. cmsg->par = capi_cmd2par(cmsg->Command, cmsg->Subcommand);
  724. cdb = bufprint(cdb, "%-26s ID=%03d #0x%04x LEN=%04d\n",
  725. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  726. ((unsigned short *) msg)[1],
  727. ((unsigned short *) msg)[3],
  728. ((unsigned short *) msg)[0]);
  729. cdb = protocol_message_2_pars(cdb, cmsg, 1);
  730. if (unlikely(cmsg != g_cmsg))
  731. kfree(cmsg);
  732. return cdb;
  733. }
  734. /**
  735. * capi_cmsg2str() - format _cmsg structure for printing
  736. * @cmsg: _cmsg structure
  737. *
  738. * Allocates a CAPI debug buffer and fills it with a printable representation
  739. * of the CAPI 2.0 message stored in @cmsg by a previous call to
  740. * capi_cmsg2message() or capi_message2cmsg().
  741. * Return value: allocated debug buffer, NULL on error
  742. * The returned buffer should be freed by a call to cdebbuf_free() after use.
  743. */
  744. _cdebbuf *capi_cmsg2str(_cmsg *cmsg)
  745. {
  746. _cdebbuf *cdb;
  747. if (!cmsg->m)
  748. return NULL; /* no message */
  749. cdb = cdebbuf_alloc();
  750. if (!cdb)
  751. return NULL;
  752. cmsg->l = 8;
  753. cmsg->p = 0;
  754. cdb = bufprint(cdb, "%s ID=%03d #0x%04x LEN=%04d\n",
  755. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  756. ((u16 *) cmsg->m)[1],
  757. ((u16 *) cmsg->m)[3],
  758. ((u16 *) cmsg->m)[0]);
  759. cdb = protocol_message_2_pars(cdb, cmsg, 1);
  760. return cdb;
  761. }
  762. int __init cdebug_init(void)
  763. {
  764. g_cmsg = kmalloc(sizeof(_cmsg), GFP_KERNEL);
  765. if (!g_cmsg)
  766. return -ENOMEM;
  767. g_debbuf = kmalloc(sizeof(_cdebbuf), GFP_KERNEL);
  768. if (!g_debbuf) {
  769. kfree(g_cmsg);
  770. return -ENOMEM;
  771. }
  772. g_debbuf->buf = kmalloc(CDEBUG_GSIZE, GFP_KERNEL);
  773. if (!g_debbuf->buf) {
  774. kfree(g_cmsg);
  775. kfree(g_debbuf);
  776. return -ENOMEM;
  777. }
  778. g_debbuf->size = CDEBUG_GSIZE;
  779. g_debbuf->buf[0] = 0;
  780. g_debbuf->p = g_debbuf->buf;
  781. g_debbuf->pos = 0;
  782. return 0;
  783. }
  784. void __exit cdebug_exit(void)
  785. {
  786. if (g_debbuf)
  787. kfree(g_debbuf->buf);
  788. kfree(g_debbuf);
  789. kfree(g_cmsg);
  790. }
  791. #else /* !CONFIG_CAPI_TRACE */
  792. static _cdebbuf g_debbuf = {"CONFIG_CAPI_TRACE not enabled", NULL, 0, 0};
  793. _cdebbuf *capi_message2str(u8 *msg)
  794. {
  795. return &g_debbuf;
  796. }
  797. _cdebbuf *capi_cmsg2str(_cmsg *cmsg)
  798. {
  799. return &g_debbuf;
  800. }
  801. void cdebbuf_free(_cdebbuf *cdb)
  802. {
  803. }
  804. int __init cdebug_init(void)
  805. {
  806. return 0;
  807. }
  808. void __exit cdebug_exit(void)
  809. {
  810. }
  811. #endif
  812. EXPORT_SYMBOL(cdebbuf_free);
  813. EXPORT_SYMBOL(capi_cmsg2message);
  814. EXPORT_SYMBOL(capi_message2cmsg);
  815. EXPORT_SYMBOL(capi_cmsg_header);
  816. EXPORT_SYMBOL(capi_cmd2str);
  817. EXPORT_SYMBOL(capi_cmsg2str);
  818. EXPORT_SYMBOL(capi_message2str);