capiutil.c 29 KB

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