capi.c 15 KB

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