capi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /*
  2. * CAPI encoder/decoder for
  3. * Portugal Telecom CAPI 2.0
  4. *
  5. * Copyright (C) 1996 Universidade de Lisboa
  6. *
  7. * Written by Pedro Roque Marques (roque@di.fc.ul.pt)
  8. *
  9. * This software may be used and distributed according to the terms of
  10. * the GNU General Public License, incorporated herein by reference.
  11. *
  12. * Not compatible with the AVM Gmbh. CAPI 2.0
  13. *
  14. */
  15. /*
  16. * Documentation:
  17. * - "Common ISDN API - Perfil Português - Versão 2.1",
  18. * Telecom Portugal, Fev 1992.
  19. * - "Common ISDN API - Especificação de protocolos para
  20. * acesso aos canais B", Inesc, Jan 1994.
  21. */
  22. /*
  23. * TODO: better decoding of Information Elements
  24. * for debug purposes mainly
  25. * encode our number in CallerPN and ConnectedPN
  26. */
  27. #include <linux/string.h>
  28. #include <linux/kernel.h>
  29. #include <linux/types.h>
  30. #include <linux/slab.h>
  31. #include <linux/mm.h>
  32. #include <linux/skbuff.h>
  33. #include <asm/io.h>
  34. #include <asm/string.h>
  35. #include <linux/isdnif.h>
  36. #include "pcbit.h"
  37. #include "edss1.h"
  38. #include "capi.h"
  39. /*
  40. * Encoding of CAPI messages
  41. *
  42. */
  43. int capi_conn_req(const char *calledPN, struct sk_buff **skb, int proto)
  44. {
  45. ushort len;
  46. /*
  47. * length
  48. * AppInfoMask - 2
  49. * BC0 - 3
  50. * BC1 - 1
  51. * Chan - 2
  52. * Keypad - 1
  53. * CPN - 1
  54. * CPSA - 1
  55. * CalledPN - 2 + strlen
  56. * CalledPSA - 1
  57. * rest... - 4
  58. * ----------------
  59. * Total 18 + strlen
  60. */
  61. len = 18 + strlen(calledPN);
  62. if (proto == ISDN_PROTO_L2_TRANS)
  63. len++;
  64. if ((*skb = dev_alloc_skb(len)) == NULL) {
  65. printk(KERN_WARNING "capi_conn_req: alloc_skb failed\n");
  66. return -1;
  67. }
  68. /* InfoElmMask */
  69. *((ushort *)skb_put(*skb, 2)) = AppInfoMask;
  70. if (proto == ISDN_PROTO_L2_TRANS)
  71. {
  72. /* Bearer Capability - Mandatory*/
  73. *(skb_put(*skb, 1)) = 3; /* BC0.Length */
  74. *(skb_put(*skb, 1)) = 0x80; /* Speech */
  75. *(skb_put(*skb, 1)) = 0x10; /* Circuit Mode */
  76. *(skb_put(*skb, 1)) = 0x23; /* A-law */
  77. }
  78. else
  79. {
  80. /* Bearer Capability - Mandatory*/
  81. *(skb_put(*skb, 1)) = 2; /* BC0.Length */
  82. *(skb_put(*skb, 1)) = 0x88; /* Digital Information */
  83. *(skb_put(*skb, 1)) = 0x90; /* BC0.Octect4 */
  84. }
  85. /* Bearer Capability - Optional*/
  86. *(skb_put(*skb, 1)) = 0; /* BC1.Length = 0 */
  87. *(skb_put(*skb, 1)) = 1; /* ChannelID.Length = 1 */
  88. *(skb_put(*skb, 1)) = 0x83; /* Basic Interface - Any Channel */
  89. *(skb_put(*skb, 1)) = 0; /* Keypad.Length = 0 */
  90. *(skb_put(*skb, 1)) = 0; /* CallingPN.Length = 0 */
  91. *(skb_put(*skb, 1)) = 0; /* CallingPSA.Length = 0 */
  92. /* Called Party Number */
  93. *(skb_put(*skb, 1)) = strlen(calledPN) + 1;
  94. *(skb_put(*skb, 1)) = 0x81;
  95. memcpy(skb_put(*skb, strlen(calledPN)), calledPN, strlen(calledPN));
  96. /* '#' */
  97. *(skb_put(*skb, 1)) = 0; /* CalledPSA.Length = 0 */
  98. /* LLC.Length = 0; */
  99. /* HLC0.Length = 0; */
  100. /* HLC1.Length = 0; */
  101. /* UTUS.Length = 0; */
  102. memset(skb_put(*skb, 4), 0, 4);
  103. return len;
  104. }
  105. int capi_conn_resp(struct pcbit_chan *chan, struct sk_buff **skb)
  106. {
  107. if ((*skb = dev_alloc_skb(5)) == NULL) {
  108. printk(KERN_WARNING "capi_conn_resp: alloc_skb failed\n");
  109. return -1;
  110. }
  111. *((ushort *)skb_put(*skb, 2)) = chan->callref;
  112. *(skb_put(*skb, 1)) = 0x01; /* ACCEPT_CALL */
  113. *(skb_put(*skb, 1)) = 0;
  114. *(skb_put(*skb, 1)) = 0;
  115. return 5;
  116. }
  117. int capi_conn_active_req(struct pcbit_chan *chan, struct sk_buff **skb)
  118. {
  119. /*
  120. * 8 bytes
  121. */
  122. if ((*skb = dev_alloc_skb(8)) == NULL) {
  123. printk(KERN_WARNING "capi_conn_active_req: alloc_skb failed\n");
  124. return -1;
  125. }
  126. *((ushort *)skb_put(*skb, 2)) = chan->callref;
  127. #ifdef DEBUG
  128. printk(KERN_DEBUG "Call Reference: %04x\n", chan->callref);
  129. #endif
  130. *(skb_put(*skb, 1)) = 0; /* BC.Length = 0; */
  131. *(skb_put(*skb, 1)) = 0; /* ConnectedPN.Length = 0 */
  132. *(skb_put(*skb, 1)) = 0; /* PSA.Length */
  133. *(skb_put(*skb, 1)) = 0; /* LLC.Length = 0; */
  134. *(skb_put(*skb, 1)) = 0; /* HLC.Length = 0; */
  135. *(skb_put(*skb, 1)) = 0; /* UTUS.Length = 0; */
  136. return 8;
  137. }
  138. int capi_conn_active_resp(struct pcbit_chan *chan, struct sk_buff **skb)
  139. {
  140. /*
  141. * 2 bytes
  142. */
  143. if ((*skb = dev_alloc_skb(2)) == NULL) {
  144. printk(KERN_WARNING "capi_conn_active_resp: alloc_skb failed\n");
  145. return -1;
  146. }
  147. *((ushort *)skb_put(*skb, 2)) = chan->callref;
  148. return 2;
  149. }
  150. int capi_select_proto_req(struct pcbit_chan *chan, struct sk_buff **skb,
  151. int outgoing)
  152. {
  153. /*
  154. * 18 bytes
  155. */
  156. if ((*skb = dev_alloc_skb(18)) == NULL) {
  157. printk(KERN_WARNING "capi_select_proto_req: alloc_skb failed\n");
  158. return -1;
  159. }
  160. *((ushort *)skb_put(*skb, 2)) = chan->callref;
  161. /* Layer2 protocol */
  162. switch (chan->proto) {
  163. case ISDN_PROTO_L2_X75I:
  164. *(skb_put(*skb, 1)) = 0x05; /* LAPB */
  165. break;
  166. case ISDN_PROTO_L2_HDLC:
  167. *(skb_put(*skb, 1)) = 0x02;
  168. break;
  169. case ISDN_PROTO_L2_TRANS:
  170. /*
  171. * Voice (a-law)
  172. */
  173. *(skb_put(*skb, 1)) = 0x06;
  174. break;
  175. default:
  176. #ifdef DEBUG
  177. printk(KERN_DEBUG "Transparent\n");
  178. #endif
  179. *(skb_put(*skb, 1)) = 0x03;
  180. break;
  181. }
  182. *(skb_put(*skb, 1)) = (outgoing ? 0x02 : 0x42); /* Don't ask */
  183. *(skb_put(*skb, 1)) = 0x00;
  184. *((ushort *) skb_put(*skb, 2)) = MRU;
  185. *(skb_put(*skb, 1)) = 0x08; /* Modulo */
  186. *(skb_put(*skb, 1)) = 0x07; /* Max Window */
  187. *(skb_put(*skb, 1)) = 0x01; /* No Layer3 Protocol */
  188. /*
  189. * 2 - layer3 MTU [10]
  190. * - Modulo [12]
  191. * - Window
  192. * - layer1 proto [14]
  193. * - bitrate
  194. * - sub-channel [16]
  195. * - layer1dataformat [17]
  196. */
  197. memset(skb_put(*skb, 8), 0, 8);
  198. return 18;
  199. }
  200. int capi_activate_transp_req(struct pcbit_chan *chan, struct sk_buff **skb)
  201. {
  202. if ((*skb = dev_alloc_skb(7)) == NULL) {
  203. printk(KERN_WARNING "capi_activate_transp_req: alloc_skb failed\n");
  204. return -1;
  205. }
  206. *((ushort *)skb_put(*skb, 2)) = chan->callref;
  207. *(skb_put(*skb, 1)) = chan->layer2link; /* Layer2 id */
  208. *(skb_put(*skb, 1)) = 0x00; /* Transmit by default */
  209. *((ushort *) skb_put(*skb, 2)) = MRU;
  210. *(skb_put(*skb, 1)) = 0x01; /* Enables reception*/
  211. return 7;
  212. }
  213. int capi_tdata_req(struct pcbit_chan *chan, struct sk_buff *skb)
  214. {
  215. ushort data_len;
  216. /*
  217. * callref - 2
  218. * layer2link - 1
  219. * wBlockLength - 2
  220. * data - 4
  221. * sernum - 1
  222. */
  223. data_len = skb->len;
  224. if (skb_headroom(skb) < 10)
  225. {
  226. printk(KERN_CRIT "No headspace (%u) on headroom %p for capi header\n", skb_headroom(skb), skb);
  227. }
  228. else
  229. {
  230. skb_push(skb, 10);
  231. }
  232. *((u16 *) (skb->data)) = chan->callref;
  233. skb->data[2] = chan->layer2link;
  234. *((u16 *) (skb->data + 3)) = data_len;
  235. chan->s_refnum = (chan->s_refnum + 1) % 8;
  236. *((u32 *) (skb->data + 5)) = chan->s_refnum;
  237. skb->data[9] = 0; /* HDLC frame number */
  238. return 10;
  239. }
  240. int capi_tdata_resp(struct pcbit_chan *chan, struct sk_buff **skb)
  241. {
  242. if ((*skb = dev_alloc_skb(4)) == NULL) {
  243. printk(KERN_WARNING "capi_tdata_resp: alloc_skb failed\n");
  244. return -1;
  245. }
  246. *((ushort *)skb_put(*skb, 2)) = chan->callref;
  247. *(skb_put(*skb, 1)) = chan->layer2link;
  248. *(skb_put(*skb, 1)) = chan->r_refnum;
  249. return (*skb)->len;
  250. }
  251. int capi_disc_req(ushort callref, struct sk_buff **skb, u_char cause)
  252. {
  253. if ((*skb = dev_alloc_skb(6)) == NULL) {
  254. printk(KERN_WARNING "capi_disc_req: alloc_skb failed\n");
  255. return -1;
  256. }
  257. *((ushort *)skb_put(*skb, 2)) = callref;
  258. *(skb_put(*skb, 1)) = 2; /* Cause.Length = 2; */
  259. *(skb_put(*skb, 1)) = 0x80;
  260. *(skb_put(*skb, 1)) = 0x80 | cause;
  261. /*
  262. * Change it: we should send 'Sic transit gloria Mundi' here ;-)
  263. */
  264. *(skb_put(*skb, 1)) = 0; /* UTUS.Length = 0; */
  265. return 6;
  266. }
  267. int capi_disc_resp(struct pcbit_chan *chan, struct sk_buff **skb)
  268. {
  269. if ((*skb = dev_alloc_skb(2)) == NULL) {
  270. printk(KERN_WARNING "capi_disc_resp: alloc_skb failed\n");
  271. return -1;
  272. }
  273. *((ushort *)skb_put(*skb, 2)) = chan->callref;
  274. return 2;
  275. }
  276. /*
  277. * Decoding of CAPI messages
  278. *
  279. */
  280. int capi_decode_conn_ind(struct pcbit_chan *chan,
  281. struct sk_buff *skb,
  282. struct callb_data *info)
  283. {
  284. int CIlen, len;
  285. /* Call Reference [CAPI] */
  286. chan->callref = *((ushort *)skb->data);
  287. skb_pull(skb, 2);
  288. #ifdef DEBUG
  289. printk(KERN_DEBUG "Call Reference: %04x\n", chan->callref);
  290. #endif
  291. /* Channel Identification */
  292. /* Expect
  293. Len = 1
  294. Octect 3 = 0100 10CC - [ 7 Basic, 4 , 2-1 chan ]
  295. */
  296. CIlen = skb->data[0];
  297. #ifdef DEBUG
  298. if (CIlen == 1) {
  299. if (((skb->data[1]) & 0xFC) == 0x48)
  300. printk(KERN_DEBUG "decode_conn_ind: chan ok\n");
  301. printk(KERN_DEBUG "phyChan = %d\n", skb->data[1] & 0x03);
  302. }
  303. else
  304. printk(KERN_DEBUG "conn_ind: CIlen = %d\n", CIlen);
  305. #endif
  306. skb_pull(skb, CIlen + 1);
  307. /* Calling Party Number */
  308. /* An "additional service" as far as Portugal Telecom is concerned */
  309. len = skb->data[0];
  310. if (len > 0) {
  311. int count = 1;
  312. #ifdef DEBUG
  313. printk(KERN_DEBUG "CPN: Octect 3 %02x\n", skb->data[1]);
  314. #endif
  315. if ((skb->data[1] & 0x80) == 0)
  316. count = 2;
  317. if (!(info->data.setup.CallingPN = kmalloc(len - count + 1, GFP_ATOMIC)))
  318. return -1;
  319. skb_copy_from_linear_data_offset(skb, count + 1,
  320. info->data.setup.CallingPN,
  321. len - count);
  322. info->data.setup.CallingPN[len - count] = 0;
  323. }
  324. else {
  325. info->data.setup.CallingPN = NULL;
  326. printk(KERN_DEBUG "NULL CallingPN\n");
  327. }
  328. skb_pull(skb, len + 1);
  329. /* Calling Party Subaddress */
  330. skb_pull(skb, skb->data[0] + 1);
  331. /* Called Party Number */
  332. len = skb->data[0];
  333. if (len > 0) {
  334. int count = 1;
  335. if ((skb->data[1] & 0x80) == 0)
  336. count = 2;
  337. if (!(info->data.setup.CalledPN = kmalloc(len - count + 1, GFP_ATOMIC)))
  338. return -1;
  339. skb_copy_from_linear_data_offset(skb, count + 1,
  340. info->data.setup.CalledPN,
  341. len - count);
  342. info->data.setup.CalledPN[len - count] = 0;
  343. }
  344. else {
  345. info->data.setup.CalledPN = NULL;
  346. printk(KERN_DEBUG "NULL CalledPN\n");
  347. }
  348. skb_pull(skb, len + 1);
  349. /* Called Party Subaddress */
  350. skb_pull(skb, skb->data[0] + 1);
  351. /* LLC */
  352. skb_pull(skb, skb->data[0] + 1);
  353. /* HLC */
  354. skb_pull(skb, skb->data[0] + 1);
  355. /* U2U */
  356. skb_pull(skb, skb->data[0] + 1);
  357. return 0;
  358. }
  359. /*
  360. * returns errcode
  361. */
  362. int capi_decode_conn_conf(struct pcbit_chan *chan, struct sk_buff *skb,
  363. int *complete)
  364. {
  365. int errcode;
  366. chan->callref = *((ushort *)skb->data); /* Update CallReference */
  367. skb_pull(skb, 2);
  368. errcode = *((ushort *) skb->data); /* read errcode */
  369. skb_pull(skb, 2);
  370. *complete = *(skb->data);
  371. skb_pull(skb, 1);
  372. /* FIX ME */
  373. /* This is actually a firmware bug */
  374. if (!*complete)
  375. {
  376. printk(KERN_DEBUG "complete=%02x\n", *complete);
  377. *complete = 1;
  378. }
  379. /* Optional Bearer Capability */
  380. skb_pull(skb, *(skb->data) + 1);
  381. /* Channel Identification */
  382. skb_pull(skb, *(skb->data) + 1);
  383. /* High Layer Compatibility follows */
  384. skb_pull(skb, *(skb->data) + 1);
  385. return errcode;
  386. }
  387. int capi_decode_conn_actv_ind(struct pcbit_chan *chan, struct sk_buff *skb)
  388. {
  389. ushort len;
  390. #ifdef DEBUG
  391. char str[32];
  392. #endif
  393. /* Yet Another Bearer Capability */
  394. skb_pull(skb, *(skb->data) + 1);
  395. /* Connected Party Number */
  396. len = *(skb->data);
  397. #ifdef DEBUG
  398. if (len > 1 && len < 31) {
  399. skb_copy_from_linear_data_offset(skb, 2, str, len - 1);
  400. str[len] = 0;
  401. printk(KERN_DEBUG "Connected Party Number: %s\n", str);
  402. }
  403. else
  404. printk(KERN_DEBUG "actv_ind CPN len = %d\n", len);
  405. #endif
  406. skb_pull(skb, len + 1);
  407. /* Connected Subaddress */
  408. skb_pull(skb, *(skb->data) + 1);
  409. /* Low Layer Capability */
  410. skb_pull(skb, *(skb->data) + 1);
  411. /* High Layer Capability */
  412. skb_pull(skb, *(skb->data) + 1);
  413. return 0;
  414. }
  415. int capi_decode_conn_actv_conf(struct pcbit_chan *chan, struct sk_buff *skb)
  416. {
  417. ushort errcode;
  418. errcode = *((ushort *)skb->data);
  419. skb_pull(skb, 2);
  420. /* Channel Identification
  421. skb_pull(skb, skb->data[0] + 1);
  422. */
  423. return errcode;
  424. }
  425. int capi_decode_sel_proto_conf(struct pcbit_chan *chan, struct sk_buff *skb)
  426. {
  427. ushort errcode;
  428. chan->layer2link = *(skb->data);
  429. skb_pull(skb, 1);
  430. errcode = *((ushort *)skb->data);
  431. skb_pull(skb, 2);
  432. return errcode;
  433. }
  434. int capi_decode_actv_trans_conf(struct pcbit_chan *chan, struct sk_buff *skb)
  435. {
  436. ushort errcode;
  437. if (chan->layer2link != *(skb->data))
  438. printk("capi_decode_actv_trans_conf: layer2link doesn't match\n");
  439. skb_pull(skb, 1);
  440. errcode = *((ushort *)skb->data);
  441. skb_pull(skb, 2);
  442. return errcode;
  443. }
  444. int capi_decode_disc_ind(struct pcbit_chan *chan, struct sk_buff *skb)
  445. {
  446. ushort len;
  447. #ifdef DEBUG
  448. int i;
  449. #endif
  450. /* Cause */
  451. len = *(skb->data);
  452. skb_pull(skb, 1);
  453. #ifdef DEBUG
  454. for (i = 0; i < len; i++)
  455. printk(KERN_DEBUG "Cause Octect %d: %02x\n", i + 3,
  456. *(skb->data + i));
  457. #endif
  458. skb_pull(skb, len);
  459. return 0;
  460. }
  461. #ifdef DEBUG
  462. int capi_decode_debug_188(u_char *hdr, ushort hdrlen)
  463. {
  464. char str[64];
  465. int len;
  466. len = hdr[0];
  467. if (len < 64 && len == hdrlen - 1) {
  468. memcpy(str, hdr + 1, hdrlen - 1);
  469. str[hdrlen - 1] = 0;
  470. printk("%s\n", str);
  471. }
  472. else
  473. printk("debug message incorrect\n");
  474. return 0;
  475. }
  476. #endif