capiutil.c 28 KB

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