layer2.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267
  1. /*
  2. *
  3. * Author Karsten Keil <kkeil@novell.com>
  4. *
  5. * Copyright 2008 by Karsten Keil <kkeil@novell.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/mISDNif.h>
  18. #include <linux/slab.h>
  19. #include "core.h"
  20. #include "fsm.h"
  21. #include "layer2.h"
  22. static u_int *debug;
  23. static
  24. struct Fsm l2fsm = {NULL, 0, 0, NULL, NULL};
  25. static char *strL2State[] =
  26. {
  27. "ST_L2_1",
  28. "ST_L2_2",
  29. "ST_L2_3",
  30. "ST_L2_4",
  31. "ST_L2_5",
  32. "ST_L2_6",
  33. "ST_L2_7",
  34. "ST_L2_8",
  35. };
  36. enum {
  37. EV_L2_UI,
  38. EV_L2_SABME,
  39. EV_L2_DISC,
  40. EV_L2_DM,
  41. EV_L2_UA,
  42. EV_L2_FRMR,
  43. EV_L2_SUPER,
  44. EV_L2_I,
  45. EV_L2_DL_DATA,
  46. EV_L2_ACK_PULL,
  47. EV_L2_DL_UNITDATA,
  48. EV_L2_DL_ESTABLISH_REQ,
  49. EV_L2_DL_RELEASE_REQ,
  50. EV_L2_MDL_ASSIGN,
  51. EV_L2_MDL_REMOVE,
  52. EV_L2_MDL_ERROR,
  53. EV_L1_DEACTIVATE,
  54. EV_L2_T200,
  55. EV_L2_T203,
  56. EV_L2_T200I,
  57. EV_L2_T203I,
  58. EV_L2_SET_OWN_BUSY,
  59. EV_L2_CLEAR_OWN_BUSY,
  60. EV_L2_FRAME_ERROR,
  61. };
  62. #define L2_EVENT_COUNT (EV_L2_FRAME_ERROR + 1)
  63. static char *strL2Event[] =
  64. {
  65. "EV_L2_UI",
  66. "EV_L2_SABME",
  67. "EV_L2_DISC",
  68. "EV_L2_DM",
  69. "EV_L2_UA",
  70. "EV_L2_FRMR",
  71. "EV_L2_SUPER",
  72. "EV_L2_I",
  73. "EV_L2_DL_DATA",
  74. "EV_L2_ACK_PULL",
  75. "EV_L2_DL_UNITDATA",
  76. "EV_L2_DL_ESTABLISH_REQ",
  77. "EV_L2_DL_RELEASE_REQ",
  78. "EV_L2_MDL_ASSIGN",
  79. "EV_L2_MDL_REMOVE",
  80. "EV_L2_MDL_ERROR",
  81. "EV_L1_DEACTIVATE",
  82. "EV_L2_T200",
  83. "EV_L2_T203",
  84. "EV_L2_T200I",
  85. "EV_L2_T203I",
  86. "EV_L2_SET_OWN_BUSY",
  87. "EV_L2_CLEAR_OWN_BUSY",
  88. "EV_L2_FRAME_ERROR",
  89. };
  90. static void
  91. l2m_debug(struct FsmInst *fi, char *fmt, ...)
  92. {
  93. struct layer2 *l2 = fi->userdata;
  94. struct va_format vaf;
  95. va_list va;
  96. if (!(*debug & DEBUG_L2_FSM))
  97. return;
  98. va_start(va, fmt);
  99. vaf.fmt = fmt;
  100. vaf.va = &va;
  101. printk(KERN_DEBUG "%s l2 (sapi %d tei %d): %pV\n",
  102. mISDNDevName4ch(&l2->ch), l2->sapi, l2->tei, &vaf);
  103. va_end(va);
  104. }
  105. inline u_int
  106. l2headersize(struct layer2 *l2, int ui)
  107. {
  108. return ((test_bit(FLG_MOD128, &l2->flag) && (!ui)) ? 2 : 1) +
  109. (test_bit(FLG_LAPD, &l2->flag) ? 2 : 1);
  110. }
  111. inline u_int
  112. l2addrsize(struct layer2 *l2)
  113. {
  114. return test_bit(FLG_LAPD, &l2->flag) ? 2 : 1;
  115. }
  116. static u_int
  117. l2_newid(struct layer2 *l2)
  118. {
  119. u_int id;
  120. id = l2->next_id++;
  121. if (id == 0x7fff)
  122. l2->next_id = 1;
  123. id <<= 16;
  124. id |= l2->tei << 8;
  125. id |= l2->sapi;
  126. return id;
  127. }
  128. static void
  129. l2up(struct layer2 *l2, u_int prim, struct sk_buff *skb)
  130. {
  131. int err;
  132. if (!l2->up)
  133. return;
  134. mISDN_HEAD_PRIM(skb) = prim;
  135. mISDN_HEAD_ID(skb) = (l2->ch.nr << 16) | l2->ch.addr;
  136. err = l2->up->send(l2->up, skb);
  137. if (err) {
  138. printk(KERN_WARNING "%s: dev %s err=%d\n", __func__,
  139. mISDNDevName4ch(&l2->ch), err);
  140. dev_kfree_skb(skb);
  141. }
  142. }
  143. static void
  144. l2up_create(struct layer2 *l2, u_int prim, int len, void *arg)
  145. {
  146. struct sk_buff *skb;
  147. struct mISDNhead *hh;
  148. int err;
  149. if (!l2->up)
  150. return;
  151. skb = mI_alloc_skb(len, GFP_ATOMIC);
  152. if (!skb)
  153. return;
  154. hh = mISDN_HEAD_P(skb);
  155. hh->prim = prim;
  156. hh->id = (l2->ch.nr << 16) | l2->ch.addr;
  157. if (len)
  158. memcpy(skb_put(skb, len), arg, len);
  159. err = l2->up->send(l2->up, skb);
  160. if (err) {
  161. printk(KERN_WARNING "%s: dev %s err=%d\n", __func__,
  162. mISDNDevName4ch(&l2->ch), err);
  163. dev_kfree_skb(skb);
  164. }
  165. }
  166. static int
  167. l2down_skb(struct layer2 *l2, struct sk_buff *skb) {
  168. int ret;
  169. ret = l2->ch.recv(l2->ch.peer, skb);
  170. if (ret && (*debug & DEBUG_L2_RECV))
  171. printk(KERN_DEBUG "l2down_skb: dev %s ret(%d)\n",
  172. mISDNDevName4ch(&l2->ch), ret);
  173. return ret;
  174. }
  175. static int
  176. l2down_raw(struct layer2 *l2, struct sk_buff *skb)
  177. {
  178. struct mISDNhead *hh = mISDN_HEAD_P(skb);
  179. if (hh->prim == PH_DATA_REQ) {
  180. if (test_and_set_bit(FLG_L1_NOTREADY, &l2->flag)) {
  181. skb_queue_tail(&l2->down_queue, skb);
  182. return 0;
  183. }
  184. l2->down_id = mISDN_HEAD_ID(skb);
  185. }
  186. return l2down_skb(l2, skb);
  187. }
  188. static int
  189. l2down(struct layer2 *l2, u_int prim, u_int id, struct sk_buff *skb)
  190. {
  191. struct mISDNhead *hh = mISDN_HEAD_P(skb);
  192. hh->prim = prim;
  193. hh->id = id;
  194. return l2down_raw(l2, skb);
  195. }
  196. static int
  197. l2down_create(struct layer2 *l2, u_int prim, u_int id, int len, void *arg)
  198. {
  199. struct sk_buff *skb;
  200. int err;
  201. struct mISDNhead *hh;
  202. skb = mI_alloc_skb(len, GFP_ATOMIC);
  203. if (!skb)
  204. return -ENOMEM;
  205. hh = mISDN_HEAD_P(skb);
  206. hh->prim = prim;
  207. hh->id = id;
  208. if (len)
  209. memcpy(skb_put(skb, len), arg, len);
  210. err = l2down_raw(l2, skb);
  211. if (err)
  212. dev_kfree_skb(skb);
  213. return err;
  214. }
  215. static int
  216. ph_data_confirm(struct layer2 *l2, struct mISDNhead *hh, struct sk_buff *skb) {
  217. struct sk_buff *nskb = skb;
  218. int ret = -EAGAIN;
  219. if (test_bit(FLG_L1_NOTREADY, &l2->flag)) {
  220. if (hh->id == l2->down_id) {
  221. nskb = skb_dequeue(&l2->down_queue);
  222. if (nskb) {
  223. l2->down_id = mISDN_HEAD_ID(nskb);
  224. if (l2down_skb(l2, nskb)) {
  225. dev_kfree_skb(nskb);
  226. l2->down_id = MISDN_ID_NONE;
  227. }
  228. } else
  229. l2->down_id = MISDN_ID_NONE;
  230. if (ret) {
  231. dev_kfree_skb(skb);
  232. ret = 0;
  233. }
  234. if (l2->down_id == MISDN_ID_NONE) {
  235. test_and_clear_bit(FLG_L1_NOTREADY, &l2->flag);
  236. mISDN_FsmEvent(&l2->l2m, EV_L2_ACK_PULL, NULL);
  237. }
  238. }
  239. }
  240. if (!test_and_set_bit(FLG_L1_NOTREADY, &l2->flag)) {
  241. nskb = skb_dequeue(&l2->down_queue);
  242. if (nskb) {
  243. l2->down_id = mISDN_HEAD_ID(nskb);
  244. if (l2down_skb(l2, nskb)) {
  245. dev_kfree_skb(nskb);
  246. l2->down_id = MISDN_ID_NONE;
  247. test_and_clear_bit(FLG_L1_NOTREADY, &l2->flag);
  248. }
  249. } else
  250. test_and_clear_bit(FLG_L1_NOTREADY, &l2->flag);
  251. }
  252. return ret;
  253. }
  254. static void
  255. l2_timeout(struct FsmInst *fi, int event, void *arg)
  256. {
  257. struct layer2 *l2 = fi->userdata;
  258. struct sk_buff *skb;
  259. struct mISDNhead *hh;
  260. skb = mI_alloc_skb(0, GFP_ATOMIC);
  261. if (!skb) {
  262. printk(KERN_WARNING "%s: L2(%d,%d) nr:%x timer %s no skb\n",
  263. mISDNDevName4ch(&l2->ch), l2->sapi, l2->tei,
  264. l2->ch.nr, event == EV_L2_T200 ? "T200" : "T203");
  265. return;
  266. }
  267. hh = mISDN_HEAD_P(skb);
  268. hh->prim = event == EV_L2_T200 ? DL_TIMER200_IND : DL_TIMER203_IND;
  269. hh->id = l2->ch.nr;
  270. if (*debug & DEBUG_TIMER)
  271. printk(KERN_DEBUG "%s: L2(%d,%d) nr:%x timer %s expired\n",
  272. mISDNDevName4ch(&l2->ch), l2->sapi, l2->tei,
  273. l2->ch.nr, event == EV_L2_T200 ? "T200" : "T203");
  274. if (l2->ch.st)
  275. l2->ch.st->own.recv(&l2->ch.st->own, skb);
  276. }
  277. static int
  278. l2mgr(struct layer2 *l2, u_int prim, void *arg) {
  279. long c = (long)arg;
  280. printk(KERN_WARNING "l2mgr: dev %s addr:%x prim %x %c\n",
  281. mISDNDevName4ch(&l2->ch), l2->id, prim, (char)c);
  282. if (test_bit(FLG_LAPD, &l2->flag) &&
  283. !test_bit(FLG_FIXED_TEI, &l2->flag)) {
  284. switch (c) {
  285. case 'C':
  286. case 'D':
  287. case 'G':
  288. case 'H':
  289. l2_tei(l2, prim, (u_long)arg);
  290. break;
  291. }
  292. }
  293. return 0;
  294. }
  295. static void
  296. set_peer_busy(struct layer2 *l2) {
  297. test_and_set_bit(FLG_PEER_BUSY, &l2->flag);
  298. if (skb_queue_len(&l2->i_queue) || skb_queue_len(&l2->ui_queue))
  299. test_and_set_bit(FLG_L2BLOCK, &l2->flag);
  300. }
  301. static void
  302. clear_peer_busy(struct layer2 *l2) {
  303. if (test_and_clear_bit(FLG_PEER_BUSY, &l2->flag))
  304. test_and_clear_bit(FLG_L2BLOCK, &l2->flag);
  305. }
  306. static void
  307. InitWin(struct layer2 *l2)
  308. {
  309. int i;
  310. for (i = 0; i < MAX_WINDOW; i++)
  311. l2->windowar[i] = NULL;
  312. }
  313. static int
  314. freewin(struct layer2 *l2)
  315. {
  316. int i, cnt = 0;
  317. for (i = 0; i < MAX_WINDOW; i++) {
  318. if (l2->windowar[i]) {
  319. cnt++;
  320. dev_kfree_skb(l2->windowar[i]);
  321. l2->windowar[i] = NULL;
  322. }
  323. }
  324. return cnt;
  325. }
  326. static void
  327. ReleaseWin(struct layer2 *l2)
  328. {
  329. int cnt = freewin(l2);
  330. if (cnt)
  331. printk(KERN_WARNING
  332. "isdnl2 freed %d skbuffs in release\n", cnt);
  333. }
  334. inline unsigned int
  335. cansend(struct layer2 *l2)
  336. {
  337. unsigned int p1;
  338. if (test_bit(FLG_MOD128, &l2->flag))
  339. p1 = (l2->vs - l2->va) % 128;
  340. else
  341. p1 = (l2->vs - l2->va) % 8;
  342. return (p1 < l2->window) && !test_bit(FLG_PEER_BUSY, &l2->flag);
  343. }
  344. inline void
  345. clear_exception(struct layer2 *l2)
  346. {
  347. test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
  348. test_and_clear_bit(FLG_REJEXC, &l2->flag);
  349. test_and_clear_bit(FLG_OWN_BUSY, &l2->flag);
  350. clear_peer_busy(l2);
  351. }
  352. static int
  353. sethdraddr(struct layer2 *l2, u_char *header, int rsp)
  354. {
  355. u_char *ptr = header;
  356. int crbit = rsp;
  357. if (test_bit(FLG_LAPD, &l2->flag)) {
  358. if (test_bit(FLG_LAPD_NET, &l2->flag))
  359. crbit = !crbit;
  360. *ptr++ = (l2->sapi << 2) | (crbit ? 2 : 0);
  361. *ptr++ = (l2->tei << 1) | 1;
  362. return 2;
  363. } else {
  364. if (test_bit(FLG_ORIG, &l2->flag))
  365. crbit = !crbit;
  366. if (crbit)
  367. *ptr++ = l2->addr.B;
  368. else
  369. *ptr++ = l2->addr.A;
  370. return 1;
  371. }
  372. }
  373. static inline void
  374. enqueue_super(struct layer2 *l2, struct sk_buff *skb)
  375. {
  376. if (l2down(l2, PH_DATA_REQ, l2_newid(l2), skb))
  377. dev_kfree_skb(skb);
  378. }
  379. static inline void
  380. enqueue_ui(struct layer2 *l2, struct sk_buff *skb)
  381. {
  382. if (l2->tm)
  383. l2_tei(l2, MDL_STATUS_UI_IND, 0);
  384. if (l2down(l2, PH_DATA_REQ, l2_newid(l2), skb))
  385. dev_kfree_skb(skb);
  386. }
  387. inline int
  388. IsUI(u_char *data)
  389. {
  390. return (data[0] & 0xef) == UI;
  391. }
  392. inline int
  393. IsUA(u_char *data)
  394. {
  395. return (data[0] & 0xef) == UA;
  396. }
  397. inline int
  398. IsDM(u_char *data)
  399. {
  400. return (data[0] & 0xef) == DM;
  401. }
  402. inline int
  403. IsDISC(u_char *data)
  404. {
  405. return (data[0] & 0xef) == DISC;
  406. }
  407. inline int
  408. IsRR(u_char *data, struct layer2 *l2)
  409. {
  410. if (test_bit(FLG_MOD128, &l2->flag))
  411. return data[0] == RR;
  412. else
  413. return (data[0] & 0xf) == 1;
  414. }
  415. inline int
  416. IsSFrame(u_char *data, struct layer2 *l2)
  417. {
  418. register u_char d = *data;
  419. if (!test_bit(FLG_MOD128, &l2->flag))
  420. d &= 0xf;
  421. return ((d & 0xf3) == 1) && ((d & 0x0c) != 0x0c);
  422. }
  423. inline int
  424. IsSABME(u_char *data, struct layer2 *l2)
  425. {
  426. u_char d = data[0] & ~0x10;
  427. return test_bit(FLG_MOD128, &l2->flag) ? d == SABME : d == SABM;
  428. }
  429. inline int
  430. IsREJ(u_char *data, struct layer2 *l2)
  431. {
  432. return test_bit(FLG_MOD128, &l2->flag) ?
  433. data[0] == REJ : (data[0] & 0xf) == REJ;
  434. }
  435. inline int
  436. IsFRMR(u_char *data)
  437. {
  438. return (data[0] & 0xef) == FRMR;
  439. }
  440. inline int
  441. IsRNR(u_char *data, struct layer2 *l2)
  442. {
  443. return test_bit(FLG_MOD128, &l2->flag) ?
  444. data[0] == RNR : (data[0] & 0xf) == RNR;
  445. }
  446. static int
  447. iframe_error(struct layer2 *l2, struct sk_buff *skb)
  448. {
  449. u_int i;
  450. int rsp = *skb->data & 0x2;
  451. i = l2addrsize(l2) + (test_bit(FLG_MOD128, &l2->flag) ? 2 : 1);
  452. if (test_bit(FLG_ORIG, &l2->flag))
  453. rsp = !rsp;
  454. if (rsp)
  455. return 'L';
  456. if (skb->len < i)
  457. return 'N';
  458. if ((skb->len - i) > l2->maxlen)
  459. return 'O';
  460. return 0;
  461. }
  462. static int
  463. super_error(struct layer2 *l2, struct sk_buff *skb)
  464. {
  465. if (skb->len != l2addrsize(l2) +
  466. (test_bit(FLG_MOD128, &l2->flag) ? 2 : 1))
  467. return 'N';
  468. return 0;
  469. }
  470. static int
  471. unnum_error(struct layer2 *l2, struct sk_buff *skb, int wantrsp)
  472. {
  473. int rsp = (*skb->data & 0x2) >> 1;
  474. if (test_bit(FLG_ORIG, &l2->flag))
  475. rsp = !rsp;
  476. if (rsp != wantrsp)
  477. return 'L';
  478. if (skb->len != l2addrsize(l2) + 1)
  479. return 'N';
  480. return 0;
  481. }
  482. static int
  483. UI_error(struct layer2 *l2, struct sk_buff *skb)
  484. {
  485. int rsp = *skb->data & 0x2;
  486. if (test_bit(FLG_ORIG, &l2->flag))
  487. rsp = !rsp;
  488. if (rsp)
  489. return 'L';
  490. if (skb->len > l2->maxlen + l2addrsize(l2) + 1)
  491. return 'O';
  492. return 0;
  493. }
  494. static int
  495. FRMR_error(struct layer2 *l2, struct sk_buff *skb)
  496. {
  497. u_int headers = l2addrsize(l2) + 1;
  498. u_char *datap = skb->data + headers;
  499. int rsp = *skb->data & 0x2;
  500. if (test_bit(FLG_ORIG, &l2->flag))
  501. rsp = !rsp;
  502. if (!rsp)
  503. return 'L';
  504. if (test_bit(FLG_MOD128, &l2->flag)) {
  505. if (skb->len < headers + 5)
  506. return 'N';
  507. else if (*debug & DEBUG_L2)
  508. l2m_debug(&l2->l2m,
  509. "FRMR information %2x %2x %2x %2x %2x",
  510. datap[0], datap[1], datap[2], datap[3], datap[4]);
  511. } else {
  512. if (skb->len < headers + 3)
  513. return 'N';
  514. else if (*debug & DEBUG_L2)
  515. l2m_debug(&l2->l2m,
  516. "FRMR information %2x %2x %2x",
  517. datap[0], datap[1], datap[2]);
  518. }
  519. return 0;
  520. }
  521. static unsigned int
  522. legalnr(struct layer2 *l2, unsigned int nr)
  523. {
  524. if (test_bit(FLG_MOD128, &l2->flag))
  525. return ((nr - l2->va) % 128) <= ((l2->vs - l2->va) % 128);
  526. else
  527. return ((nr - l2->va) % 8) <= ((l2->vs - l2->va) % 8);
  528. }
  529. static void
  530. setva(struct layer2 *l2, unsigned int nr)
  531. {
  532. struct sk_buff *skb;
  533. while (l2->va != nr) {
  534. l2->va++;
  535. if (test_bit(FLG_MOD128, &l2->flag))
  536. l2->va %= 128;
  537. else
  538. l2->va %= 8;
  539. if (l2->windowar[l2->sow]) {
  540. skb_trim(l2->windowar[l2->sow], 0);
  541. skb_queue_tail(&l2->tmp_queue, l2->windowar[l2->sow]);
  542. l2->windowar[l2->sow] = NULL;
  543. }
  544. l2->sow = (l2->sow + 1) % l2->window;
  545. }
  546. skb = skb_dequeue(&l2->tmp_queue);
  547. while (skb) {
  548. dev_kfree_skb(skb);
  549. skb = skb_dequeue(&l2->tmp_queue);
  550. }
  551. }
  552. static void
  553. send_uframe(struct layer2 *l2, struct sk_buff *skb, u_char cmd, u_char cr)
  554. {
  555. u_char tmp[MAX_L2HEADER_LEN];
  556. int i;
  557. i = sethdraddr(l2, tmp, cr);
  558. tmp[i++] = cmd;
  559. if (skb)
  560. skb_trim(skb, 0);
  561. else {
  562. skb = mI_alloc_skb(i, GFP_ATOMIC);
  563. if (!skb) {
  564. printk(KERN_WARNING "%s: can't alloc skbuff in %s\n",
  565. mISDNDevName4ch(&l2->ch), __func__);
  566. return;
  567. }
  568. }
  569. memcpy(skb_put(skb, i), tmp, i);
  570. enqueue_super(l2, skb);
  571. }
  572. inline u_char
  573. get_PollFlag(struct layer2 *l2, struct sk_buff *skb)
  574. {
  575. return skb->data[l2addrsize(l2)] & 0x10;
  576. }
  577. inline u_char
  578. get_PollFlagFree(struct layer2 *l2, struct sk_buff *skb)
  579. {
  580. u_char PF;
  581. PF = get_PollFlag(l2, skb);
  582. dev_kfree_skb(skb);
  583. return PF;
  584. }
  585. inline void
  586. start_t200(struct layer2 *l2, int i)
  587. {
  588. mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, i);
  589. test_and_set_bit(FLG_T200_RUN, &l2->flag);
  590. }
  591. inline void
  592. restart_t200(struct layer2 *l2, int i)
  593. {
  594. mISDN_FsmRestartTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, i);
  595. test_and_set_bit(FLG_T200_RUN, &l2->flag);
  596. }
  597. inline void
  598. stop_t200(struct layer2 *l2, int i)
  599. {
  600. if (test_and_clear_bit(FLG_T200_RUN, &l2->flag))
  601. mISDN_FsmDelTimer(&l2->t200, i);
  602. }
  603. inline void
  604. st5_dl_release_l2l3(struct layer2 *l2)
  605. {
  606. int pr;
  607. if (test_and_clear_bit(FLG_PEND_REL, &l2->flag))
  608. pr = DL_RELEASE_CNF;
  609. else
  610. pr = DL_RELEASE_IND;
  611. l2up_create(l2, pr, 0, NULL);
  612. }
  613. inline void
  614. lapb_dl_release_l2l3(struct layer2 *l2, int f)
  615. {
  616. if (test_bit(FLG_LAPB, &l2->flag))
  617. l2down_create(l2, PH_DEACTIVATE_REQ, l2_newid(l2), 0, NULL);
  618. l2up_create(l2, f, 0, NULL);
  619. }
  620. static void
  621. establishlink(struct FsmInst *fi)
  622. {
  623. struct layer2 *l2 = fi->userdata;
  624. u_char cmd;
  625. clear_exception(l2);
  626. l2->rc = 0;
  627. cmd = (test_bit(FLG_MOD128, &l2->flag) ? SABME : SABM) | 0x10;
  628. send_uframe(l2, NULL, cmd, CMD);
  629. mISDN_FsmDelTimer(&l2->t203, 1);
  630. restart_t200(l2, 1);
  631. test_and_clear_bit(FLG_PEND_REL, &l2->flag);
  632. freewin(l2);
  633. mISDN_FsmChangeState(fi, ST_L2_5);
  634. }
  635. static void
  636. l2_mdl_error_ua(struct FsmInst *fi, int event, void *arg)
  637. {
  638. struct sk_buff *skb = arg;
  639. struct layer2 *l2 = fi->userdata;
  640. if (get_PollFlagFree(l2, skb))
  641. l2mgr(l2, MDL_ERROR_IND, (void *) 'C');
  642. else
  643. l2mgr(l2, MDL_ERROR_IND, (void *) 'D');
  644. }
  645. static void
  646. l2_mdl_error_dm(struct FsmInst *fi, int event, void *arg)
  647. {
  648. struct sk_buff *skb = arg;
  649. struct layer2 *l2 = fi->userdata;
  650. if (get_PollFlagFree(l2, skb))
  651. l2mgr(l2, MDL_ERROR_IND, (void *) 'B');
  652. else {
  653. l2mgr(l2, MDL_ERROR_IND, (void *) 'E');
  654. establishlink(fi);
  655. test_and_clear_bit(FLG_L3_INIT, &l2->flag);
  656. }
  657. }
  658. static void
  659. l2_st8_mdl_error_dm(struct FsmInst *fi, int event, void *arg)
  660. {
  661. struct sk_buff *skb = arg;
  662. struct layer2 *l2 = fi->userdata;
  663. if (get_PollFlagFree(l2, skb))
  664. l2mgr(l2, MDL_ERROR_IND, (void *) 'B');
  665. else
  666. l2mgr(l2, MDL_ERROR_IND, (void *) 'E');
  667. establishlink(fi);
  668. test_and_clear_bit(FLG_L3_INIT, &l2->flag);
  669. }
  670. static void
  671. l2_go_st3(struct FsmInst *fi, int event, void *arg)
  672. {
  673. dev_kfree_skb((struct sk_buff *)arg);
  674. mISDN_FsmChangeState(fi, ST_L2_3);
  675. }
  676. static void
  677. l2_mdl_assign(struct FsmInst *fi, int event, void *arg)
  678. {
  679. struct layer2 *l2 = fi->userdata;
  680. mISDN_FsmChangeState(fi, ST_L2_3);
  681. dev_kfree_skb((struct sk_buff *)arg);
  682. l2_tei(l2, MDL_ASSIGN_IND, 0);
  683. }
  684. static void
  685. l2_queue_ui_assign(struct FsmInst *fi, int event, void *arg)
  686. {
  687. struct layer2 *l2 = fi->userdata;
  688. struct sk_buff *skb = arg;
  689. skb_queue_tail(&l2->ui_queue, skb);
  690. mISDN_FsmChangeState(fi, ST_L2_2);
  691. l2_tei(l2, MDL_ASSIGN_IND, 0);
  692. }
  693. static void
  694. l2_queue_ui(struct FsmInst *fi, int event, void *arg)
  695. {
  696. struct layer2 *l2 = fi->userdata;
  697. struct sk_buff *skb = arg;
  698. skb_queue_tail(&l2->ui_queue, skb);
  699. }
  700. static void
  701. tx_ui(struct layer2 *l2)
  702. {
  703. struct sk_buff *skb;
  704. u_char header[MAX_L2HEADER_LEN];
  705. int i;
  706. i = sethdraddr(l2, header, CMD);
  707. if (test_bit(FLG_LAPD_NET, &l2->flag))
  708. header[1] = 0xff; /* tei 127 */
  709. header[i++] = UI;
  710. while ((skb = skb_dequeue(&l2->ui_queue))) {
  711. memcpy(skb_push(skb, i), header, i);
  712. enqueue_ui(l2, skb);
  713. }
  714. }
  715. static void
  716. l2_send_ui(struct FsmInst *fi, int event, void *arg)
  717. {
  718. struct layer2 *l2 = fi->userdata;
  719. struct sk_buff *skb = arg;
  720. skb_queue_tail(&l2->ui_queue, skb);
  721. tx_ui(l2);
  722. }
  723. static void
  724. l2_got_ui(struct FsmInst *fi, int event, void *arg)
  725. {
  726. struct layer2 *l2 = fi->userdata;
  727. struct sk_buff *skb = arg;
  728. skb_pull(skb, l2headersize(l2, 1));
  729. /*
  730. * in states 1-3 for broadcast
  731. */
  732. if (l2->tm)
  733. l2_tei(l2, MDL_STATUS_UI_IND, 0);
  734. l2up(l2, DL_UNITDATA_IND, skb);
  735. }
  736. static void
  737. l2_establish(struct FsmInst *fi, int event, void *arg)
  738. {
  739. struct sk_buff *skb = arg;
  740. struct layer2 *l2 = fi->userdata;
  741. establishlink(fi);
  742. test_and_set_bit(FLG_L3_INIT, &l2->flag);
  743. dev_kfree_skb(skb);
  744. }
  745. static void
  746. l2_discard_i_setl3(struct FsmInst *fi, int event, void *arg)
  747. {
  748. struct sk_buff *skb = arg;
  749. struct layer2 *l2 = fi->userdata;
  750. skb_queue_purge(&l2->i_queue);
  751. test_and_set_bit(FLG_L3_INIT, &l2->flag);
  752. test_and_clear_bit(FLG_PEND_REL, &l2->flag);
  753. dev_kfree_skb(skb);
  754. }
  755. static void
  756. l2_l3_reestablish(struct FsmInst *fi, int event, void *arg)
  757. {
  758. struct sk_buff *skb = arg;
  759. struct layer2 *l2 = fi->userdata;
  760. skb_queue_purge(&l2->i_queue);
  761. establishlink(fi);
  762. test_and_set_bit(FLG_L3_INIT, &l2->flag);
  763. dev_kfree_skb(skb);
  764. }
  765. static void
  766. l2_release(struct FsmInst *fi, int event, void *arg)
  767. {
  768. struct layer2 *l2 = fi->userdata;
  769. struct sk_buff *skb = arg;
  770. skb_trim(skb, 0);
  771. l2up(l2, DL_RELEASE_CNF, skb);
  772. }
  773. static void
  774. l2_pend_rel(struct FsmInst *fi, int event, void *arg)
  775. {
  776. struct sk_buff *skb = arg;
  777. struct layer2 *l2 = fi->userdata;
  778. test_and_set_bit(FLG_PEND_REL, &l2->flag);
  779. dev_kfree_skb(skb);
  780. }
  781. static void
  782. l2_disconnect(struct FsmInst *fi, int event, void *arg)
  783. {
  784. struct layer2 *l2 = fi->userdata;
  785. struct sk_buff *skb = arg;
  786. skb_queue_purge(&l2->i_queue);
  787. freewin(l2);
  788. mISDN_FsmChangeState(fi, ST_L2_6);
  789. l2->rc = 0;
  790. send_uframe(l2, NULL, DISC | 0x10, CMD);
  791. mISDN_FsmDelTimer(&l2->t203, 1);
  792. restart_t200(l2, 2);
  793. if (skb)
  794. dev_kfree_skb(skb);
  795. }
  796. static void
  797. l2_start_multi(struct FsmInst *fi, int event, void *arg)
  798. {
  799. struct layer2 *l2 = fi->userdata;
  800. struct sk_buff *skb = arg;
  801. l2->vs = 0;
  802. l2->va = 0;
  803. l2->vr = 0;
  804. l2->sow = 0;
  805. clear_exception(l2);
  806. send_uframe(l2, NULL, UA | get_PollFlag(l2, skb), RSP);
  807. mISDN_FsmChangeState(fi, ST_L2_7);
  808. mISDN_FsmAddTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 3);
  809. skb_trim(skb, 0);
  810. l2up(l2, DL_ESTABLISH_IND, skb);
  811. if (l2->tm)
  812. l2_tei(l2, MDL_STATUS_UP_IND, 0);
  813. }
  814. static void
  815. l2_send_UA(struct FsmInst *fi, int event, void *arg)
  816. {
  817. struct layer2 *l2 = fi->userdata;
  818. struct sk_buff *skb = arg;
  819. send_uframe(l2, skb, UA | get_PollFlag(l2, skb), RSP);
  820. }
  821. static void
  822. l2_send_DM(struct FsmInst *fi, int event, void *arg)
  823. {
  824. struct layer2 *l2 = fi->userdata;
  825. struct sk_buff *skb = arg;
  826. send_uframe(l2, skb, DM | get_PollFlag(l2, skb), RSP);
  827. }
  828. static void
  829. l2_restart_multi(struct FsmInst *fi, int event, void *arg)
  830. {
  831. struct layer2 *l2 = fi->userdata;
  832. struct sk_buff *skb = arg;
  833. int est = 0;
  834. send_uframe(l2, skb, UA | get_PollFlag(l2, skb), RSP);
  835. l2mgr(l2, MDL_ERROR_IND, (void *) 'F');
  836. if (l2->vs != l2->va) {
  837. skb_queue_purge(&l2->i_queue);
  838. est = 1;
  839. }
  840. clear_exception(l2);
  841. l2->vs = 0;
  842. l2->va = 0;
  843. l2->vr = 0;
  844. l2->sow = 0;
  845. mISDN_FsmChangeState(fi, ST_L2_7);
  846. stop_t200(l2, 3);
  847. mISDN_FsmRestartTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 3);
  848. if (est)
  849. l2up_create(l2, DL_ESTABLISH_IND, 0, NULL);
  850. /* mISDN_queue_data(&l2->inst, l2->inst.id | MSG_BROADCAST,
  851. * MGR_SHORTSTATUS | INDICATION, SSTATUS_L2_ESTABLISHED,
  852. * 0, NULL, 0);
  853. */
  854. if (skb_queue_len(&l2->i_queue) && cansend(l2))
  855. mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
  856. }
  857. static void
  858. l2_stop_multi(struct FsmInst *fi, int event, void *arg)
  859. {
  860. struct layer2 *l2 = fi->userdata;
  861. struct sk_buff *skb = arg;
  862. mISDN_FsmChangeState(fi, ST_L2_4);
  863. mISDN_FsmDelTimer(&l2->t203, 3);
  864. stop_t200(l2, 4);
  865. send_uframe(l2, skb, UA | get_PollFlag(l2, skb), RSP);
  866. skb_queue_purge(&l2->i_queue);
  867. freewin(l2);
  868. lapb_dl_release_l2l3(l2, DL_RELEASE_IND);
  869. if (l2->tm)
  870. l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
  871. }
  872. static void
  873. l2_connected(struct FsmInst *fi, int event, void *arg)
  874. {
  875. struct layer2 *l2 = fi->userdata;
  876. struct sk_buff *skb = arg;
  877. int pr = -1;
  878. if (!get_PollFlag(l2, skb)) {
  879. l2_mdl_error_ua(fi, event, arg);
  880. return;
  881. }
  882. dev_kfree_skb(skb);
  883. if (test_and_clear_bit(FLG_PEND_REL, &l2->flag))
  884. l2_disconnect(fi, event, NULL);
  885. if (test_and_clear_bit(FLG_L3_INIT, &l2->flag)) {
  886. pr = DL_ESTABLISH_CNF;
  887. } else if (l2->vs != l2->va) {
  888. skb_queue_purge(&l2->i_queue);
  889. pr = DL_ESTABLISH_IND;
  890. }
  891. stop_t200(l2, 5);
  892. l2->vr = 0;
  893. l2->vs = 0;
  894. l2->va = 0;
  895. l2->sow = 0;
  896. mISDN_FsmChangeState(fi, ST_L2_7);
  897. mISDN_FsmAddTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 4);
  898. if (pr != -1)
  899. l2up_create(l2, pr, 0, NULL);
  900. if (skb_queue_len(&l2->i_queue) && cansend(l2))
  901. mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
  902. if (l2->tm)
  903. l2_tei(l2, MDL_STATUS_UP_IND, 0);
  904. }
  905. static void
  906. l2_released(struct FsmInst *fi, int event, void *arg)
  907. {
  908. struct layer2 *l2 = fi->userdata;
  909. struct sk_buff *skb = arg;
  910. if (!get_PollFlag(l2, skb)) {
  911. l2_mdl_error_ua(fi, event, arg);
  912. return;
  913. }
  914. dev_kfree_skb(skb);
  915. stop_t200(l2, 6);
  916. lapb_dl_release_l2l3(l2, DL_RELEASE_CNF);
  917. mISDN_FsmChangeState(fi, ST_L2_4);
  918. if (l2->tm)
  919. l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
  920. }
  921. static void
  922. l2_reestablish(struct FsmInst *fi, int event, void *arg)
  923. {
  924. struct layer2 *l2 = fi->userdata;
  925. struct sk_buff *skb = arg;
  926. if (!get_PollFlagFree(l2, skb)) {
  927. establishlink(fi);
  928. test_and_set_bit(FLG_L3_INIT, &l2->flag);
  929. }
  930. }
  931. static void
  932. l2_st5_dm_release(struct FsmInst *fi, int event, void *arg)
  933. {
  934. struct layer2 *l2 = fi->userdata;
  935. struct sk_buff *skb = arg;
  936. if (get_PollFlagFree(l2, skb)) {
  937. stop_t200(l2, 7);
  938. if (!test_bit(FLG_L3_INIT, &l2->flag))
  939. skb_queue_purge(&l2->i_queue);
  940. if (test_bit(FLG_LAPB, &l2->flag))
  941. l2down_create(l2, PH_DEACTIVATE_REQ,
  942. l2_newid(l2), 0, NULL);
  943. st5_dl_release_l2l3(l2);
  944. mISDN_FsmChangeState(fi, ST_L2_4);
  945. if (l2->tm)
  946. l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
  947. }
  948. }
  949. static void
  950. l2_st6_dm_release(struct FsmInst *fi, int event, void *arg)
  951. {
  952. struct layer2 *l2 = fi->userdata;
  953. struct sk_buff *skb = arg;
  954. if (get_PollFlagFree(l2, skb)) {
  955. stop_t200(l2, 8);
  956. lapb_dl_release_l2l3(l2, DL_RELEASE_CNF);
  957. mISDN_FsmChangeState(fi, ST_L2_4);
  958. if (l2->tm)
  959. l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
  960. }
  961. }
  962. static void
  963. enquiry_cr(struct layer2 *l2, u_char typ, u_char cr, u_char pf)
  964. {
  965. struct sk_buff *skb;
  966. u_char tmp[MAX_L2HEADER_LEN];
  967. int i;
  968. i = sethdraddr(l2, tmp, cr);
  969. if (test_bit(FLG_MOD128, &l2->flag)) {
  970. tmp[i++] = typ;
  971. tmp[i++] = (l2->vr << 1) | (pf ? 1 : 0);
  972. } else
  973. tmp[i++] = (l2->vr << 5) | typ | (pf ? 0x10 : 0);
  974. skb = mI_alloc_skb(i, GFP_ATOMIC);
  975. if (!skb) {
  976. printk(KERN_WARNING "%s: isdnl2 can't alloc sbbuff in %s\n",
  977. mISDNDevName4ch(&l2->ch), __func__);
  978. return;
  979. }
  980. memcpy(skb_put(skb, i), tmp, i);
  981. enqueue_super(l2, skb);
  982. }
  983. inline void
  984. enquiry_response(struct layer2 *l2)
  985. {
  986. if (test_bit(FLG_OWN_BUSY, &l2->flag))
  987. enquiry_cr(l2, RNR, RSP, 1);
  988. else
  989. enquiry_cr(l2, RR, RSP, 1);
  990. test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
  991. }
  992. inline void
  993. transmit_enquiry(struct layer2 *l2)
  994. {
  995. if (test_bit(FLG_OWN_BUSY, &l2->flag))
  996. enquiry_cr(l2, RNR, CMD, 1);
  997. else
  998. enquiry_cr(l2, RR, CMD, 1);
  999. test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
  1000. start_t200(l2, 9);
  1001. }
  1002. static void
  1003. nrerrorrecovery(struct FsmInst *fi)
  1004. {
  1005. struct layer2 *l2 = fi->userdata;
  1006. l2mgr(l2, MDL_ERROR_IND, (void *) 'J');
  1007. establishlink(fi);
  1008. test_and_clear_bit(FLG_L3_INIT, &l2->flag);
  1009. }
  1010. static void
  1011. invoke_retransmission(struct layer2 *l2, unsigned int nr)
  1012. {
  1013. u_int p1;
  1014. if (l2->vs != nr) {
  1015. while (l2->vs != nr) {
  1016. (l2->vs)--;
  1017. if (test_bit(FLG_MOD128, &l2->flag)) {
  1018. l2->vs %= 128;
  1019. p1 = (l2->vs - l2->va) % 128;
  1020. } else {
  1021. l2->vs %= 8;
  1022. p1 = (l2->vs - l2->va) % 8;
  1023. }
  1024. p1 = (p1 + l2->sow) % l2->window;
  1025. if (l2->windowar[p1])
  1026. skb_queue_head(&l2->i_queue, l2->windowar[p1]);
  1027. else
  1028. printk(KERN_WARNING
  1029. "%s: windowar[%d] is NULL\n",
  1030. mISDNDevName4ch(&l2->ch), p1);
  1031. l2->windowar[p1] = NULL;
  1032. }
  1033. mISDN_FsmEvent(&l2->l2m, EV_L2_ACK_PULL, NULL);
  1034. }
  1035. }
  1036. static void
  1037. l2_st7_got_super(struct FsmInst *fi, int event, void *arg)
  1038. {
  1039. struct layer2 *l2 = fi->userdata;
  1040. struct sk_buff *skb = arg;
  1041. int PollFlag, rsp, typ = RR;
  1042. unsigned int nr;
  1043. rsp = *skb->data & 0x2;
  1044. if (test_bit(FLG_ORIG, &l2->flag))
  1045. rsp = !rsp;
  1046. skb_pull(skb, l2addrsize(l2));
  1047. if (IsRNR(skb->data, l2)) {
  1048. set_peer_busy(l2);
  1049. typ = RNR;
  1050. } else
  1051. clear_peer_busy(l2);
  1052. if (IsREJ(skb->data, l2))
  1053. typ = REJ;
  1054. if (test_bit(FLG_MOD128, &l2->flag)) {
  1055. PollFlag = (skb->data[1] & 0x1) == 0x1;
  1056. nr = skb->data[1] >> 1;
  1057. } else {
  1058. PollFlag = (skb->data[0] & 0x10);
  1059. nr = (skb->data[0] >> 5) & 0x7;
  1060. }
  1061. dev_kfree_skb(skb);
  1062. if (PollFlag) {
  1063. if (rsp)
  1064. l2mgr(l2, MDL_ERROR_IND, (void *) 'A');
  1065. else
  1066. enquiry_response(l2);
  1067. }
  1068. if (legalnr(l2, nr)) {
  1069. if (typ == REJ) {
  1070. setva(l2, nr);
  1071. invoke_retransmission(l2, nr);
  1072. stop_t200(l2, 10);
  1073. if (mISDN_FsmAddTimer(&l2->t203, l2->T203,
  1074. EV_L2_T203, NULL, 6))
  1075. l2m_debug(&l2->l2m, "Restart T203 ST7 REJ");
  1076. } else if ((nr == l2->vs) && (typ == RR)) {
  1077. setva(l2, nr);
  1078. stop_t200(l2, 11);
  1079. mISDN_FsmRestartTimer(&l2->t203, l2->T203,
  1080. EV_L2_T203, NULL, 7);
  1081. } else if ((l2->va != nr) || (typ == RNR)) {
  1082. setva(l2, nr);
  1083. if (typ != RR)
  1084. mISDN_FsmDelTimer(&l2->t203, 9);
  1085. restart_t200(l2, 12);
  1086. }
  1087. if (skb_queue_len(&l2->i_queue) && (typ == RR))
  1088. mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
  1089. } else
  1090. nrerrorrecovery(fi);
  1091. }
  1092. static void
  1093. l2_feed_i_if_reest(struct FsmInst *fi, int event, void *arg)
  1094. {
  1095. struct layer2 *l2 = fi->userdata;
  1096. struct sk_buff *skb = arg;
  1097. if (!test_bit(FLG_L3_INIT, &l2->flag))
  1098. skb_queue_tail(&l2->i_queue, skb);
  1099. else
  1100. dev_kfree_skb(skb);
  1101. }
  1102. static void
  1103. l2_feed_i_pull(struct FsmInst *fi, int event, void *arg)
  1104. {
  1105. struct layer2 *l2 = fi->userdata;
  1106. struct sk_buff *skb = arg;
  1107. skb_queue_tail(&l2->i_queue, skb);
  1108. mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
  1109. }
  1110. static void
  1111. l2_feed_iqueue(struct FsmInst *fi, int event, void *arg)
  1112. {
  1113. struct layer2 *l2 = fi->userdata;
  1114. struct sk_buff *skb = arg;
  1115. skb_queue_tail(&l2->i_queue, skb);
  1116. }
  1117. static void
  1118. l2_got_iframe(struct FsmInst *fi, int event, void *arg)
  1119. {
  1120. struct layer2 *l2 = fi->userdata;
  1121. struct sk_buff *skb = arg;
  1122. int PollFlag, i;
  1123. u_int ns, nr;
  1124. i = l2addrsize(l2);
  1125. if (test_bit(FLG_MOD128, &l2->flag)) {
  1126. PollFlag = ((skb->data[i + 1] & 0x1) == 0x1);
  1127. ns = skb->data[i] >> 1;
  1128. nr = (skb->data[i + 1] >> 1) & 0x7f;
  1129. } else {
  1130. PollFlag = (skb->data[i] & 0x10);
  1131. ns = (skb->data[i] >> 1) & 0x7;
  1132. nr = (skb->data[i] >> 5) & 0x7;
  1133. }
  1134. if (test_bit(FLG_OWN_BUSY, &l2->flag)) {
  1135. dev_kfree_skb(skb);
  1136. if (PollFlag)
  1137. enquiry_response(l2);
  1138. } else {
  1139. if (l2->vr == ns) {
  1140. l2->vr++;
  1141. if (test_bit(FLG_MOD128, &l2->flag))
  1142. l2->vr %= 128;
  1143. else
  1144. l2->vr %= 8;
  1145. test_and_clear_bit(FLG_REJEXC, &l2->flag);
  1146. if (PollFlag)
  1147. enquiry_response(l2);
  1148. else
  1149. test_and_set_bit(FLG_ACK_PEND, &l2->flag);
  1150. skb_pull(skb, l2headersize(l2, 0));
  1151. l2up(l2, DL_DATA_IND, skb);
  1152. } else {
  1153. /* n(s)!=v(r) */
  1154. dev_kfree_skb(skb);
  1155. if (test_and_set_bit(FLG_REJEXC, &l2->flag)) {
  1156. if (PollFlag)
  1157. enquiry_response(l2);
  1158. } else {
  1159. enquiry_cr(l2, REJ, RSP, PollFlag);
  1160. test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
  1161. }
  1162. }
  1163. }
  1164. if (legalnr(l2, nr)) {
  1165. if (!test_bit(FLG_PEER_BUSY, &l2->flag) &&
  1166. (fi->state == ST_L2_7)) {
  1167. if (nr == l2->vs) {
  1168. stop_t200(l2, 13);
  1169. mISDN_FsmRestartTimer(&l2->t203, l2->T203,
  1170. EV_L2_T203, NULL, 7);
  1171. } else if (nr != l2->va)
  1172. restart_t200(l2, 14);
  1173. }
  1174. setva(l2, nr);
  1175. } else {
  1176. nrerrorrecovery(fi);
  1177. return;
  1178. }
  1179. if (skb_queue_len(&l2->i_queue) && (fi->state == ST_L2_7))
  1180. mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
  1181. if (test_and_clear_bit(FLG_ACK_PEND, &l2->flag))
  1182. enquiry_cr(l2, RR, RSP, 0);
  1183. }
  1184. static void
  1185. l2_got_tei(struct FsmInst *fi, int event, void *arg)
  1186. {
  1187. struct layer2 *l2 = fi->userdata;
  1188. u_int info;
  1189. l2->tei = (signed char)(long)arg;
  1190. set_channel_address(&l2->ch, l2->sapi, l2->tei);
  1191. info = DL_INFO_L2_CONNECT;
  1192. l2up_create(l2, DL_INFORMATION_IND, sizeof(info), &info);
  1193. if (fi->state == ST_L2_3) {
  1194. establishlink(fi);
  1195. test_and_set_bit(FLG_L3_INIT, &l2->flag);
  1196. } else
  1197. mISDN_FsmChangeState(fi, ST_L2_4);
  1198. if (skb_queue_len(&l2->ui_queue))
  1199. tx_ui(l2);
  1200. }
  1201. static void
  1202. l2_st5_tout_200(struct FsmInst *fi, int event, void *arg)
  1203. {
  1204. struct layer2 *l2 = fi->userdata;
  1205. if (test_bit(FLG_LAPD, &l2->flag) &&
  1206. test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
  1207. mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
  1208. } else if (l2->rc == l2->N200) {
  1209. mISDN_FsmChangeState(fi, ST_L2_4);
  1210. test_and_clear_bit(FLG_T200_RUN, &l2->flag);
  1211. skb_queue_purge(&l2->i_queue);
  1212. l2mgr(l2, MDL_ERROR_IND, (void *) 'G');
  1213. if (test_bit(FLG_LAPB, &l2->flag))
  1214. l2down_create(l2, PH_DEACTIVATE_REQ,
  1215. l2_newid(l2), 0, NULL);
  1216. st5_dl_release_l2l3(l2);
  1217. if (l2->tm)
  1218. l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
  1219. } else {
  1220. l2->rc++;
  1221. mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
  1222. send_uframe(l2, NULL, (test_bit(FLG_MOD128, &l2->flag) ?
  1223. SABME : SABM) | 0x10, CMD);
  1224. }
  1225. }
  1226. static void
  1227. l2_st6_tout_200(struct FsmInst *fi, int event, void *arg)
  1228. {
  1229. struct layer2 *l2 = fi->userdata;
  1230. if (test_bit(FLG_LAPD, &l2->flag) &&
  1231. test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
  1232. mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
  1233. } else if (l2->rc == l2->N200) {
  1234. mISDN_FsmChangeState(fi, ST_L2_4);
  1235. test_and_clear_bit(FLG_T200_RUN, &l2->flag);
  1236. l2mgr(l2, MDL_ERROR_IND, (void *) 'H');
  1237. lapb_dl_release_l2l3(l2, DL_RELEASE_CNF);
  1238. if (l2->tm)
  1239. l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
  1240. } else {
  1241. l2->rc++;
  1242. mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200,
  1243. NULL, 9);
  1244. send_uframe(l2, NULL, DISC | 0x10, CMD);
  1245. }
  1246. }
  1247. static void
  1248. l2_st7_tout_200(struct FsmInst *fi, int event, void *arg)
  1249. {
  1250. struct layer2 *l2 = fi->userdata;
  1251. if (test_bit(FLG_LAPD, &l2->flag) &&
  1252. test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
  1253. mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
  1254. return;
  1255. }
  1256. test_and_clear_bit(FLG_T200_RUN, &l2->flag);
  1257. l2->rc = 0;
  1258. mISDN_FsmChangeState(fi, ST_L2_8);
  1259. transmit_enquiry(l2);
  1260. l2->rc++;
  1261. }
  1262. static void
  1263. l2_st8_tout_200(struct FsmInst *fi, int event, void *arg)
  1264. {
  1265. struct layer2 *l2 = fi->userdata;
  1266. if (test_bit(FLG_LAPD, &l2->flag) &&
  1267. test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
  1268. mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 9);
  1269. return;
  1270. }
  1271. test_and_clear_bit(FLG_T200_RUN, &l2->flag);
  1272. if (l2->rc == l2->N200) {
  1273. l2mgr(l2, MDL_ERROR_IND, (void *) 'I');
  1274. establishlink(fi);
  1275. test_and_clear_bit(FLG_L3_INIT, &l2->flag);
  1276. } else {
  1277. transmit_enquiry(l2);
  1278. l2->rc++;
  1279. }
  1280. }
  1281. static void
  1282. l2_st7_tout_203(struct FsmInst *fi, int event, void *arg)
  1283. {
  1284. struct layer2 *l2 = fi->userdata;
  1285. if (test_bit(FLG_LAPD, &l2->flag) &&
  1286. test_bit(FLG_DCHAN_BUSY, &l2->flag)) {
  1287. mISDN_FsmAddTimer(&l2->t203, l2->T203, EV_L2_T203, NULL, 9);
  1288. return;
  1289. }
  1290. mISDN_FsmChangeState(fi, ST_L2_8);
  1291. transmit_enquiry(l2);
  1292. l2->rc = 0;
  1293. }
  1294. static void
  1295. l2_pull_iqueue(struct FsmInst *fi, int event, void *arg)
  1296. {
  1297. struct layer2 *l2 = fi->userdata;
  1298. struct sk_buff *skb, *nskb;
  1299. u_char header[MAX_L2HEADER_LEN];
  1300. u_int i, p1;
  1301. if (!cansend(l2))
  1302. return;
  1303. skb = skb_dequeue(&l2->i_queue);
  1304. if (!skb)
  1305. return;
  1306. i = sethdraddr(l2, header, CMD);
  1307. if (test_bit(FLG_MOD128, &l2->flag)) {
  1308. header[i++] = l2->vs << 1;
  1309. header[i++] = l2->vr << 1;
  1310. } else
  1311. header[i++] = (l2->vr << 5) | (l2->vs << 1);
  1312. nskb = skb_realloc_headroom(skb, i);
  1313. if (!nskb) {
  1314. printk(KERN_WARNING "%s: no headroom(%d) copy for IFrame\n",
  1315. mISDNDevName4ch(&l2->ch), i);
  1316. skb_queue_head(&l2->i_queue, skb);
  1317. return;
  1318. }
  1319. if (test_bit(FLG_MOD128, &l2->flag)) {
  1320. p1 = (l2->vs - l2->va) % 128;
  1321. l2->vs = (l2->vs + 1) % 128;
  1322. } else {
  1323. p1 = (l2->vs - l2->va) % 8;
  1324. l2->vs = (l2->vs + 1) % 8;
  1325. }
  1326. p1 = (p1 + l2->sow) % l2->window;
  1327. if (l2->windowar[p1]) {
  1328. printk(KERN_WARNING "%s: l2 try overwrite ack queue entry %d\n",
  1329. mISDNDevName4ch(&l2->ch), p1);
  1330. dev_kfree_skb(l2->windowar[p1]);
  1331. }
  1332. l2->windowar[p1] = skb;
  1333. memcpy(skb_push(nskb, i), header, i);
  1334. l2down(l2, PH_DATA_REQ, l2_newid(l2), nskb);
  1335. test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
  1336. if (!test_and_set_bit(FLG_T200_RUN, &l2->flag)) {
  1337. mISDN_FsmDelTimer(&l2->t203, 13);
  1338. mISDN_FsmAddTimer(&l2->t200, l2->T200, EV_L2_T200, NULL, 11);
  1339. }
  1340. }
  1341. static void
  1342. l2_st8_got_super(struct FsmInst *fi, int event, void *arg)
  1343. {
  1344. struct layer2 *l2 = fi->userdata;
  1345. struct sk_buff *skb = arg;
  1346. int PollFlag, rsp, rnr = 0;
  1347. unsigned int nr;
  1348. rsp = *skb->data & 0x2;
  1349. if (test_bit(FLG_ORIG, &l2->flag))
  1350. rsp = !rsp;
  1351. skb_pull(skb, l2addrsize(l2));
  1352. if (IsRNR(skb->data, l2)) {
  1353. set_peer_busy(l2);
  1354. rnr = 1;
  1355. } else
  1356. clear_peer_busy(l2);
  1357. if (test_bit(FLG_MOD128, &l2->flag)) {
  1358. PollFlag = (skb->data[1] & 0x1) == 0x1;
  1359. nr = skb->data[1] >> 1;
  1360. } else {
  1361. PollFlag = (skb->data[0] & 0x10);
  1362. nr = (skb->data[0] >> 5) & 0x7;
  1363. }
  1364. dev_kfree_skb(skb);
  1365. if (rsp && PollFlag) {
  1366. if (legalnr(l2, nr)) {
  1367. if (rnr) {
  1368. restart_t200(l2, 15);
  1369. } else {
  1370. stop_t200(l2, 16);
  1371. mISDN_FsmAddTimer(&l2->t203, l2->T203,
  1372. EV_L2_T203, NULL, 5);
  1373. setva(l2, nr);
  1374. }
  1375. invoke_retransmission(l2, nr);
  1376. mISDN_FsmChangeState(fi, ST_L2_7);
  1377. if (skb_queue_len(&l2->i_queue) && cansend(l2))
  1378. mISDN_FsmEvent(fi, EV_L2_ACK_PULL, NULL);
  1379. } else
  1380. nrerrorrecovery(fi);
  1381. } else {
  1382. if (!rsp && PollFlag)
  1383. enquiry_response(l2);
  1384. if (legalnr(l2, nr))
  1385. setva(l2, nr);
  1386. else
  1387. nrerrorrecovery(fi);
  1388. }
  1389. }
  1390. static void
  1391. l2_got_FRMR(struct FsmInst *fi, int event, void *arg)
  1392. {
  1393. struct layer2 *l2 = fi->userdata;
  1394. struct sk_buff *skb = arg;
  1395. skb_pull(skb, l2addrsize(l2) + 1);
  1396. if (!(skb->data[0] & 1) || ((skb->data[0] & 3) == 1) || /* I or S */
  1397. (IsUA(skb->data) && (fi->state == ST_L2_7))) {
  1398. l2mgr(l2, MDL_ERROR_IND, (void *) 'K');
  1399. establishlink(fi);
  1400. test_and_clear_bit(FLG_L3_INIT, &l2->flag);
  1401. }
  1402. dev_kfree_skb(skb);
  1403. }
  1404. static void
  1405. l2_st24_tei_remove(struct FsmInst *fi, int event, void *arg)
  1406. {
  1407. struct layer2 *l2 = fi->userdata;
  1408. skb_queue_purge(&l2->ui_queue);
  1409. l2->tei = GROUP_TEI;
  1410. mISDN_FsmChangeState(fi, ST_L2_1);
  1411. }
  1412. static void
  1413. l2_st3_tei_remove(struct FsmInst *fi, int event, void *arg)
  1414. {
  1415. struct layer2 *l2 = fi->userdata;
  1416. skb_queue_purge(&l2->ui_queue);
  1417. l2->tei = GROUP_TEI;
  1418. l2up_create(l2, DL_RELEASE_IND, 0, NULL);
  1419. mISDN_FsmChangeState(fi, ST_L2_1);
  1420. }
  1421. static void
  1422. l2_st5_tei_remove(struct FsmInst *fi, int event, void *arg)
  1423. {
  1424. struct layer2 *l2 = fi->userdata;
  1425. skb_queue_purge(&l2->i_queue);
  1426. skb_queue_purge(&l2->ui_queue);
  1427. freewin(l2);
  1428. l2->tei = GROUP_TEI;
  1429. stop_t200(l2, 17);
  1430. st5_dl_release_l2l3(l2);
  1431. mISDN_FsmChangeState(fi, ST_L2_1);
  1432. }
  1433. static void
  1434. l2_st6_tei_remove(struct FsmInst *fi, int event, void *arg)
  1435. {
  1436. struct layer2 *l2 = fi->userdata;
  1437. skb_queue_purge(&l2->ui_queue);
  1438. l2->tei = GROUP_TEI;
  1439. stop_t200(l2, 18);
  1440. l2up_create(l2, DL_RELEASE_IND, 0, NULL);
  1441. mISDN_FsmChangeState(fi, ST_L2_1);
  1442. }
  1443. static void
  1444. l2_tei_remove(struct FsmInst *fi, int event, void *arg)
  1445. {
  1446. struct layer2 *l2 = fi->userdata;
  1447. skb_queue_purge(&l2->i_queue);
  1448. skb_queue_purge(&l2->ui_queue);
  1449. freewin(l2);
  1450. l2->tei = GROUP_TEI;
  1451. stop_t200(l2, 17);
  1452. mISDN_FsmDelTimer(&l2->t203, 19);
  1453. l2up_create(l2, DL_RELEASE_IND, 0, NULL);
  1454. /* mISDN_queue_data(&l2->inst, l2->inst.id | MSG_BROADCAST,
  1455. * MGR_SHORTSTATUS_IND, SSTATUS_L2_RELEASED,
  1456. * 0, NULL, 0);
  1457. */
  1458. mISDN_FsmChangeState(fi, ST_L2_1);
  1459. }
  1460. static void
  1461. l2_st14_persistent_da(struct FsmInst *fi, int event, void *arg)
  1462. {
  1463. struct layer2 *l2 = fi->userdata;
  1464. struct sk_buff *skb = arg;
  1465. skb_queue_purge(&l2->i_queue);
  1466. skb_queue_purge(&l2->ui_queue);
  1467. if (test_and_clear_bit(FLG_ESTAB_PEND, &l2->flag))
  1468. l2up(l2, DL_RELEASE_IND, skb);
  1469. else
  1470. dev_kfree_skb(skb);
  1471. }
  1472. static void
  1473. l2_st5_persistent_da(struct FsmInst *fi, int event, void *arg)
  1474. {
  1475. struct layer2 *l2 = fi->userdata;
  1476. struct sk_buff *skb = arg;
  1477. skb_queue_purge(&l2->i_queue);
  1478. skb_queue_purge(&l2->ui_queue);
  1479. freewin(l2);
  1480. stop_t200(l2, 19);
  1481. st5_dl_release_l2l3(l2);
  1482. mISDN_FsmChangeState(fi, ST_L2_4);
  1483. if (l2->tm)
  1484. l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
  1485. dev_kfree_skb(skb);
  1486. }
  1487. static void
  1488. l2_st6_persistent_da(struct FsmInst *fi, int event, void *arg)
  1489. {
  1490. struct layer2 *l2 = fi->userdata;
  1491. struct sk_buff *skb = arg;
  1492. skb_queue_purge(&l2->ui_queue);
  1493. stop_t200(l2, 20);
  1494. l2up(l2, DL_RELEASE_CNF, skb);
  1495. mISDN_FsmChangeState(fi, ST_L2_4);
  1496. if (l2->tm)
  1497. l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
  1498. }
  1499. static void
  1500. l2_persistent_da(struct FsmInst *fi, int event, void *arg)
  1501. {
  1502. struct layer2 *l2 = fi->userdata;
  1503. struct sk_buff *skb = arg;
  1504. skb_queue_purge(&l2->i_queue);
  1505. skb_queue_purge(&l2->ui_queue);
  1506. freewin(l2);
  1507. stop_t200(l2, 19);
  1508. mISDN_FsmDelTimer(&l2->t203, 19);
  1509. l2up(l2, DL_RELEASE_IND, skb);
  1510. mISDN_FsmChangeState(fi, ST_L2_4);
  1511. if (l2->tm)
  1512. l2_tei(l2, MDL_STATUS_DOWN_IND, 0);
  1513. }
  1514. static void
  1515. l2_set_own_busy(struct FsmInst *fi, int event, void *arg)
  1516. {
  1517. struct layer2 *l2 = fi->userdata;
  1518. struct sk_buff *skb = arg;
  1519. if (!test_and_set_bit(FLG_OWN_BUSY, &l2->flag)) {
  1520. enquiry_cr(l2, RNR, RSP, 0);
  1521. test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
  1522. }
  1523. if (skb)
  1524. dev_kfree_skb(skb);
  1525. }
  1526. static void
  1527. l2_clear_own_busy(struct FsmInst *fi, int event, void *arg)
  1528. {
  1529. struct layer2 *l2 = fi->userdata;
  1530. struct sk_buff *skb = arg;
  1531. if (!test_and_clear_bit(FLG_OWN_BUSY, &l2->flag)) {
  1532. enquiry_cr(l2, RR, RSP, 0);
  1533. test_and_clear_bit(FLG_ACK_PEND, &l2->flag);
  1534. }
  1535. if (skb)
  1536. dev_kfree_skb(skb);
  1537. }
  1538. static void
  1539. l2_frame_error(struct FsmInst *fi, int event, void *arg)
  1540. {
  1541. struct layer2 *l2 = fi->userdata;
  1542. l2mgr(l2, MDL_ERROR_IND, arg);
  1543. }
  1544. static void
  1545. l2_frame_error_reest(struct FsmInst *fi, int event, void *arg)
  1546. {
  1547. struct layer2 *l2 = fi->userdata;
  1548. l2mgr(l2, MDL_ERROR_IND, arg);
  1549. establishlink(fi);
  1550. test_and_clear_bit(FLG_L3_INIT, &l2->flag);
  1551. }
  1552. static struct FsmNode L2FnList[] =
  1553. {
  1554. {ST_L2_1, EV_L2_DL_ESTABLISH_REQ, l2_mdl_assign},
  1555. {ST_L2_2, EV_L2_DL_ESTABLISH_REQ, l2_go_st3},
  1556. {ST_L2_4, EV_L2_DL_ESTABLISH_REQ, l2_establish},
  1557. {ST_L2_5, EV_L2_DL_ESTABLISH_REQ, l2_discard_i_setl3},
  1558. {ST_L2_7, EV_L2_DL_ESTABLISH_REQ, l2_l3_reestablish},
  1559. {ST_L2_8, EV_L2_DL_ESTABLISH_REQ, l2_l3_reestablish},
  1560. {ST_L2_4, EV_L2_DL_RELEASE_REQ, l2_release},
  1561. {ST_L2_5, EV_L2_DL_RELEASE_REQ, l2_pend_rel},
  1562. {ST_L2_7, EV_L2_DL_RELEASE_REQ, l2_disconnect},
  1563. {ST_L2_8, EV_L2_DL_RELEASE_REQ, l2_disconnect},
  1564. {ST_L2_5, EV_L2_DL_DATA, l2_feed_i_if_reest},
  1565. {ST_L2_7, EV_L2_DL_DATA, l2_feed_i_pull},
  1566. {ST_L2_8, EV_L2_DL_DATA, l2_feed_iqueue},
  1567. {ST_L2_1, EV_L2_DL_UNITDATA, l2_queue_ui_assign},
  1568. {ST_L2_2, EV_L2_DL_UNITDATA, l2_queue_ui},
  1569. {ST_L2_3, EV_L2_DL_UNITDATA, l2_queue_ui},
  1570. {ST_L2_4, EV_L2_DL_UNITDATA, l2_send_ui},
  1571. {ST_L2_5, EV_L2_DL_UNITDATA, l2_send_ui},
  1572. {ST_L2_6, EV_L2_DL_UNITDATA, l2_send_ui},
  1573. {ST_L2_7, EV_L2_DL_UNITDATA, l2_send_ui},
  1574. {ST_L2_8, EV_L2_DL_UNITDATA, l2_send_ui},
  1575. {ST_L2_1, EV_L2_MDL_ASSIGN, l2_got_tei},
  1576. {ST_L2_2, EV_L2_MDL_ASSIGN, l2_got_tei},
  1577. {ST_L2_3, EV_L2_MDL_ASSIGN, l2_got_tei},
  1578. {ST_L2_2, EV_L2_MDL_ERROR, l2_st24_tei_remove},
  1579. {ST_L2_3, EV_L2_MDL_ERROR, l2_st3_tei_remove},
  1580. {ST_L2_4, EV_L2_MDL_REMOVE, l2_st24_tei_remove},
  1581. {ST_L2_5, EV_L2_MDL_REMOVE, l2_st5_tei_remove},
  1582. {ST_L2_6, EV_L2_MDL_REMOVE, l2_st6_tei_remove},
  1583. {ST_L2_7, EV_L2_MDL_REMOVE, l2_tei_remove},
  1584. {ST_L2_8, EV_L2_MDL_REMOVE, l2_tei_remove},
  1585. {ST_L2_4, EV_L2_SABME, l2_start_multi},
  1586. {ST_L2_5, EV_L2_SABME, l2_send_UA},
  1587. {ST_L2_6, EV_L2_SABME, l2_send_DM},
  1588. {ST_L2_7, EV_L2_SABME, l2_restart_multi},
  1589. {ST_L2_8, EV_L2_SABME, l2_restart_multi},
  1590. {ST_L2_4, EV_L2_DISC, l2_send_DM},
  1591. {ST_L2_5, EV_L2_DISC, l2_send_DM},
  1592. {ST_L2_6, EV_L2_DISC, l2_send_UA},
  1593. {ST_L2_7, EV_L2_DISC, l2_stop_multi},
  1594. {ST_L2_8, EV_L2_DISC, l2_stop_multi},
  1595. {ST_L2_4, EV_L2_UA, l2_mdl_error_ua},
  1596. {ST_L2_5, EV_L2_UA, l2_connected},
  1597. {ST_L2_6, EV_L2_UA, l2_released},
  1598. {ST_L2_7, EV_L2_UA, l2_mdl_error_ua},
  1599. {ST_L2_8, EV_L2_UA, l2_mdl_error_ua},
  1600. {ST_L2_4, EV_L2_DM, l2_reestablish},
  1601. {ST_L2_5, EV_L2_DM, l2_st5_dm_release},
  1602. {ST_L2_6, EV_L2_DM, l2_st6_dm_release},
  1603. {ST_L2_7, EV_L2_DM, l2_mdl_error_dm},
  1604. {ST_L2_8, EV_L2_DM, l2_st8_mdl_error_dm},
  1605. {ST_L2_1, EV_L2_UI, l2_got_ui},
  1606. {ST_L2_2, EV_L2_UI, l2_got_ui},
  1607. {ST_L2_3, EV_L2_UI, l2_got_ui},
  1608. {ST_L2_4, EV_L2_UI, l2_got_ui},
  1609. {ST_L2_5, EV_L2_UI, l2_got_ui},
  1610. {ST_L2_6, EV_L2_UI, l2_got_ui},
  1611. {ST_L2_7, EV_L2_UI, l2_got_ui},
  1612. {ST_L2_8, EV_L2_UI, l2_got_ui},
  1613. {ST_L2_7, EV_L2_FRMR, l2_got_FRMR},
  1614. {ST_L2_8, EV_L2_FRMR, l2_got_FRMR},
  1615. {ST_L2_7, EV_L2_SUPER, l2_st7_got_super},
  1616. {ST_L2_8, EV_L2_SUPER, l2_st8_got_super},
  1617. {ST_L2_7, EV_L2_I, l2_got_iframe},
  1618. {ST_L2_8, EV_L2_I, l2_got_iframe},
  1619. {ST_L2_5, EV_L2_T200, l2_timeout},
  1620. {ST_L2_6, EV_L2_T200, l2_timeout},
  1621. {ST_L2_7, EV_L2_T200, l2_timeout},
  1622. {ST_L2_8, EV_L2_T200, l2_timeout},
  1623. {ST_L2_7, EV_L2_T203, l2_timeout},
  1624. {ST_L2_5, EV_L2_T200I, l2_st5_tout_200},
  1625. {ST_L2_6, EV_L2_T200I, l2_st6_tout_200},
  1626. {ST_L2_7, EV_L2_T200I, l2_st7_tout_200},
  1627. {ST_L2_8, EV_L2_T200I, l2_st8_tout_200},
  1628. {ST_L2_7, EV_L2_T203I, l2_st7_tout_203},
  1629. {ST_L2_7, EV_L2_ACK_PULL, l2_pull_iqueue},
  1630. {ST_L2_7, EV_L2_SET_OWN_BUSY, l2_set_own_busy},
  1631. {ST_L2_8, EV_L2_SET_OWN_BUSY, l2_set_own_busy},
  1632. {ST_L2_7, EV_L2_CLEAR_OWN_BUSY, l2_clear_own_busy},
  1633. {ST_L2_8, EV_L2_CLEAR_OWN_BUSY, l2_clear_own_busy},
  1634. {ST_L2_4, EV_L2_FRAME_ERROR, l2_frame_error},
  1635. {ST_L2_5, EV_L2_FRAME_ERROR, l2_frame_error},
  1636. {ST_L2_6, EV_L2_FRAME_ERROR, l2_frame_error},
  1637. {ST_L2_7, EV_L2_FRAME_ERROR, l2_frame_error_reest},
  1638. {ST_L2_8, EV_L2_FRAME_ERROR, l2_frame_error_reest},
  1639. {ST_L2_1, EV_L1_DEACTIVATE, l2_st14_persistent_da},
  1640. {ST_L2_2, EV_L1_DEACTIVATE, l2_st24_tei_remove},
  1641. {ST_L2_3, EV_L1_DEACTIVATE, l2_st3_tei_remove},
  1642. {ST_L2_4, EV_L1_DEACTIVATE, l2_st14_persistent_da},
  1643. {ST_L2_5, EV_L1_DEACTIVATE, l2_st5_persistent_da},
  1644. {ST_L2_6, EV_L1_DEACTIVATE, l2_st6_persistent_da},
  1645. {ST_L2_7, EV_L1_DEACTIVATE, l2_persistent_da},
  1646. {ST_L2_8, EV_L1_DEACTIVATE, l2_persistent_da},
  1647. };
  1648. static int
  1649. ph_data_indication(struct layer2 *l2, struct mISDNhead *hh, struct sk_buff *skb)
  1650. {
  1651. u_char *datap = skb->data;
  1652. int ret = -EINVAL;
  1653. int psapi, ptei;
  1654. u_int l;
  1655. int c = 0;
  1656. l = l2addrsize(l2);
  1657. if (skb->len <= l) {
  1658. mISDN_FsmEvent(&l2->l2m, EV_L2_FRAME_ERROR, (void *) 'N');
  1659. return ret;
  1660. }
  1661. if (test_bit(FLG_LAPD, &l2->flag)) { /* Maybe not needed */
  1662. psapi = *datap++;
  1663. ptei = *datap++;
  1664. if ((psapi & 1) || !(ptei & 1)) {
  1665. printk(KERN_WARNING
  1666. "%s l2 D-channel frame wrong EA0/EA1\n",
  1667. mISDNDevName4ch(&l2->ch));
  1668. return ret;
  1669. }
  1670. psapi >>= 2;
  1671. ptei >>= 1;
  1672. if (psapi != l2->sapi) {
  1673. /* not our business */
  1674. if (*debug & DEBUG_L2)
  1675. printk(KERN_DEBUG "%s: sapi %d/%d mismatch\n",
  1676. mISDNDevName4ch(&l2->ch), psapi,
  1677. l2->sapi);
  1678. dev_kfree_skb(skb);
  1679. return 0;
  1680. }
  1681. if ((ptei != l2->tei) && (ptei != GROUP_TEI)) {
  1682. /* not our business */
  1683. if (*debug & DEBUG_L2)
  1684. printk(KERN_DEBUG "%s: tei %d/%d mismatch\n",
  1685. mISDNDevName4ch(&l2->ch), ptei, l2->tei);
  1686. dev_kfree_skb(skb);
  1687. return 0;
  1688. }
  1689. } else
  1690. datap += l;
  1691. if (!(*datap & 1)) { /* I-Frame */
  1692. c = iframe_error(l2, skb);
  1693. if (!c)
  1694. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_I, skb);
  1695. } else if (IsSFrame(datap, l2)) { /* S-Frame */
  1696. c = super_error(l2, skb);
  1697. if (!c)
  1698. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_SUPER, skb);
  1699. } else if (IsUI(datap)) {
  1700. c = UI_error(l2, skb);
  1701. if (!c)
  1702. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_UI, skb);
  1703. } else if (IsSABME(datap, l2)) {
  1704. c = unnum_error(l2, skb, CMD);
  1705. if (!c)
  1706. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_SABME, skb);
  1707. } else if (IsUA(datap)) {
  1708. c = unnum_error(l2, skb, RSP);
  1709. if (!c)
  1710. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_UA, skb);
  1711. } else if (IsDISC(datap)) {
  1712. c = unnum_error(l2, skb, CMD);
  1713. if (!c)
  1714. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DISC, skb);
  1715. } else if (IsDM(datap)) {
  1716. c = unnum_error(l2, skb, RSP);
  1717. if (!c)
  1718. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DM, skb);
  1719. } else if (IsFRMR(datap)) {
  1720. c = FRMR_error(l2, skb);
  1721. if (!c)
  1722. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_FRMR, skb);
  1723. } else
  1724. c = 'L';
  1725. if (c) {
  1726. printk(KERN_WARNING "%s:l2 D-channel frame error %c\n",
  1727. mISDNDevName4ch(&l2->ch), c);
  1728. mISDN_FsmEvent(&l2->l2m, EV_L2_FRAME_ERROR, (void *)(long)c);
  1729. }
  1730. return ret;
  1731. }
  1732. static int
  1733. l2_send(struct mISDNchannel *ch, struct sk_buff *skb)
  1734. {
  1735. struct layer2 *l2 = container_of(ch, struct layer2, ch);
  1736. struct mISDNhead *hh = mISDN_HEAD_P(skb);
  1737. int ret = -EINVAL;
  1738. if (*debug & DEBUG_L2_RECV)
  1739. printk(KERN_DEBUG "%s: %s prim(%x) id(%x) sapi(%d) tei(%d)\n",
  1740. __func__, mISDNDevName4ch(&l2->ch), hh->prim, hh->id,
  1741. l2->sapi, l2->tei);
  1742. if (hh->prim == DL_INTERN_MSG) {
  1743. struct mISDNhead *chh = hh + 1; /* saved copy */
  1744. *hh = *chh;
  1745. if (*debug & DEBUG_L2_RECV)
  1746. printk(KERN_DEBUG "%s: prim(%x) id(%x) internal msg\n",
  1747. mISDNDevName4ch(&l2->ch), hh->prim, hh->id);
  1748. }
  1749. switch (hh->prim) {
  1750. case PH_DATA_IND:
  1751. ret = ph_data_indication(l2, hh, skb);
  1752. break;
  1753. case PH_DATA_CNF:
  1754. ret = ph_data_confirm(l2, hh, skb);
  1755. break;
  1756. case PH_ACTIVATE_IND:
  1757. test_and_set_bit(FLG_L1_ACTIV, &l2->flag);
  1758. l2up_create(l2, MPH_ACTIVATE_IND, 0, NULL);
  1759. if (test_and_clear_bit(FLG_ESTAB_PEND, &l2->flag))
  1760. ret = mISDN_FsmEvent(&l2->l2m,
  1761. EV_L2_DL_ESTABLISH_REQ, skb);
  1762. break;
  1763. case PH_DEACTIVATE_IND:
  1764. test_and_clear_bit(FLG_L1_ACTIV, &l2->flag);
  1765. l2up_create(l2, MPH_DEACTIVATE_IND, 0, NULL);
  1766. ret = mISDN_FsmEvent(&l2->l2m, EV_L1_DEACTIVATE, skb);
  1767. break;
  1768. case MPH_INFORMATION_IND:
  1769. if (!l2->up)
  1770. break;
  1771. ret = l2->up->send(l2->up, skb);
  1772. break;
  1773. case DL_DATA_REQ:
  1774. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DL_DATA, skb);
  1775. break;
  1776. case DL_UNITDATA_REQ:
  1777. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DL_UNITDATA, skb);
  1778. break;
  1779. case DL_ESTABLISH_REQ:
  1780. if (test_bit(FLG_LAPB, &l2->flag))
  1781. test_and_set_bit(FLG_ORIG, &l2->flag);
  1782. if (test_bit(FLG_L1_ACTIV, &l2->flag)) {
  1783. if (test_bit(FLG_LAPD, &l2->flag) ||
  1784. test_bit(FLG_ORIG, &l2->flag))
  1785. ret = mISDN_FsmEvent(&l2->l2m,
  1786. EV_L2_DL_ESTABLISH_REQ, skb);
  1787. } else {
  1788. if (test_bit(FLG_LAPD, &l2->flag) ||
  1789. test_bit(FLG_ORIG, &l2->flag)) {
  1790. test_and_set_bit(FLG_ESTAB_PEND,
  1791. &l2->flag);
  1792. }
  1793. ret = l2down(l2, PH_ACTIVATE_REQ, l2_newid(l2),
  1794. skb);
  1795. }
  1796. break;
  1797. case DL_RELEASE_REQ:
  1798. if (test_bit(FLG_LAPB, &l2->flag))
  1799. l2down_create(l2, PH_DEACTIVATE_REQ,
  1800. l2_newid(l2), 0, NULL);
  1801. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_DL_RELEASE_REQ,
  1802. skb);
  1803. break;
  1804. case DL_TIMER200_IND:
  1805. mISDN_FsmEvent(&l2->l2m, EV_L2_T200I, NULL);
  1806. break;
  1807. case DL_TIMER203_IND:
  1808. mISDN_FsmEvent(&l2->l2m, EV_L2_T203I, NULL);
  1809. break;
  1810. default:
  1811. if (*debug & DEBUG_L2)
  1812. l2m_debug(&l2->l2m, "l2 unknown pr %04x",
  1813. hh->prim);
  1814. }
  1815. if (ret) {
  1816. dev_kfree_skb(skb);
  1817. ret = 0;
  1818. }
  1819. return ret;
  1820. }
  1821. int
  1822. tei_l2(struct layer2 *l2, u_int cmd, u_long arg)
  1823. {
  1824. int ret = -EINVAL;
  1825. if (*debug & DEBUG_L2_TEI)
  1826. printk(KERN_DEBUG "%s: cmd(%x) in %s\n",
  1827. mISDNDevName4ch(&l2->ch), cmd, __func__);
  1828. switch (cmd) {
  1829. case (MDL_ASSIGN_REQ):
  1830. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_ASSIGN, (void *)arg);
  1831. break;
  1832. case (MDL_REMOVE_REQ):
  1833. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_REMOVE, NULL);
  1834. break;
  1835. case (MDL_ERROR_IND):
  1836. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_ERROR, NULL);
  1837. break;
  1838. case (MDL_ERROR_RSP):
  1839. /* ETS 300-125 5.3.2.1 Test: TC13010 */
  1840. printk(KERN_NOTICE "%s: MDL_ERROR|REQ (tei_l2)\n",
  1841. mISDNDevName4ch(&l2->ch));
  1842. ret = mISDN_FsmEvent(&l2->l2m, EV_L2_MDL_ERROR, NULL);
  1843. break;
  1844. }
  1845. return ret;
  1846. }
  1847. static void
  1848. release_l2(struct layer2 *l2)
  1849. {
  1850. mISDN_FsmDelTimer(&l2->t200, 21);
  1851. mISDN_FsmDelTimer(&l2->t203, 16);
  1852. skb_queue_purge(&l2->i_queue);
  1853. skb_queue_purge(&l2->ui_queue);
  1854. skb_queue_purge(&l2->down_queue);
  1855. ReleaseWin(l2);
  1856. if (test_bit(FLG_LAPD, &l2->flag)) {
  1857. TEIrelease(l2);
  1858. if (l2->ch.st)
  1859. l2->ch.st->dev->D.ctrl(&l2->ch.st->dev->D,
  1860. CLOSE_CHANNEL, NULL);
  1861. }
  1862. kfree(l2);
  1863. }
  1864. static int
  1865. l2_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
  1866. {
  1867. struct layer2 *l2 = container_of(ch, struct layer2, ch);
  1868. u_int info;
  1869. if (*debug & DEBUG_L2_CTRL)
  1870. printk(KERN_DEBUG "%s: %s cmd(%x)\n",
  1871. mISDNDevName4ch(ch), __func__, cmd);
  1872. switch (cmd) {
  1873. case OPEN_CHANNEL:
  1874. if (test_bit(FLG_LAPD, &l2->flag)) {
  1875. set_channel_address(&l2->ch, l2->sapi, l2->tei);
  1876. info = DL_INFO_L2_CONNECT;
  1877. l2up_create(l2, DL_INFORMATION_IND,
  1878. sizeof(info), &info);
  1879. }
  1880. break;
  1881. case CLOSE_CHANNEL:
  1882. if (l2->ch.peer)
  1883. l2->ch.peer->ctrl(l2->ch.peer, CLOSE_CHANNEL, NULL);
  1884. release_l2(l2);
  1885. break;
  1886. }
  1887. return 0;
  1888. }
  1889. struct layer2 *
  1890. create_l2(struct mISDNchannel *ch, u_int protocol, u_long options, int tei,
  1891. int sapi)
  1892. {
  1893. struct layer2 *l2;
  1894. struct channel_req rq;
  1895. l2 = kzalloc(sizeof(struct layer2), GFP_KERNEL);
  1896. if (!l2) {
  1897. printk(KERN_ERR "kzalloc layer2 failed\n");
  1898. return NULL;
  1899. }
  1900. l2->next_id = 1;
  1901. l2->down_id = MISDN_ID_NONE;
  1902. l2->up = ch;
  1903. l2->ch.st = ch->st;
  1904. l2->ch.send = l2_send;
  1905. l2->ch.ctrl = l2_ctrl;
  1906. switch (protocol) {
  1907. case ISDN_P_LAPD_NT:
  1908. test_and_set_bit(FLG_LAPD, &l2->flag);
  1909. test_and_set_bit(FLG_LAPD_NET, &l2->flag);
  1910. test_and_set_bit(FLG_MOD128, &l2->flag);
  1911. l2->sapi = sapi;
  1912. l2->maxlen = MAX_DFRAME_LEN;
  1913. if (test_bit(OPTION_L2_PMX, &options))
  1914. l2->window = 7;
  1915. else
  1916. l2->window = 1;
  1917. if (test_bit(OPTION_L2_PTP, &options))
  1918. test_and_set_bit(FLG_PTP, &l2->flag);
  1919. if (test_bit(OPTION_L2_FIXEDTEI, &options))
  1920. test_and_set_bit(FLG_FIXED_TEI, &l2->flag);
  1921. l2->tei = tei;
  1922. l2->T200 = 1000;
  1923. l2->N200 = 3;
  1924. l2->T203 = 10000;
  1925. if (test_bit(OPTION_L2_PMX, &options))
  1926. rq.protocol = ISDN_P_NT_E1;
  1927. else
  1928. rq.protocol = ISDN_P_NT_S0;
  1929. rq.adr.channel = 0;
  1930. l2->ch.st->dev->D.ctrl(&l2->ch.st->dev->D, OPEN_CHANNEL, &rq);
  1931. break;
  1932. case ISDN_P_LAPD_TE:
  1933. test_and_set_bit(FLG_LAPD, &l2->flag);
  1934. test_and_set_bit(FLG_MOD128, &l2->flag);
  1935. test_and_set_bit(FLG_ORIG, &l2->flag);
  1936. l2->sapi = sapi;
  1937. l2->maxlen = MAX_DFRAME_LEN;
  1938. if (test_bit(OPTION_L2_PMX, &options))
  1939. l2->window = 7;
  1940. else
  1941. l2->window = 1;
  1942. if (test_bit(OPTION_L2_PTP, &options))
  1943. test_and_set_bit(FLG_PTP, &l2->flag);
  1944. if (test_bit(OPTION_L2_FIXEDTEI, &options))
  1945. test_and_set_bit(FLG_FIXED_TEI, &l2->flag);
  1946. l2->tei = tei;
  1947. l2->T200 = 1000;
  1948. l2->N200 = 3;
  1949. l2->T203 = 10000;
  1950. if (test_bit(OPTION_L2_PMX, &options))
  1951. rq.protocol = ISDN_P_TE_E1;
  1952. else
  1953. rq.protocol = ISDN_P_TE_S0;
  1954. rq.adr.channel = 0;
  1955. l2->ch.st->dev->D.ctrl(&l2->ch.st->dev->D, OPEN_CHANNEL, &rq);
  1956. break;
  1957. case ISDN_P_B_X75SLP:
  1958. test_and_set_bit(FLG_LAPB, &l2->flag);
  1959. l2->window = 7;
  1960. l2->maxlen = MAX_DATA_SIZE;
  1961. l2->T200 = 1000;
  1962. l2->N200 = 4;
  1963. l2->T203 = 5000;
  1964. l2->addr.A = 3;
  1965. l2->addr.B = 1;
  1966. break;
  1967. default:
  1968. printk(KERN_ERR "layer2 create failed prt %x\n",
  1969. protocol);
  1970. kfree(l2);
  1971. return NULL;
  1972. }
  1973. skb_queue_head_init(&l2->i_queue);
  1974. skb_queue_head_init(&l2->ui_queue);
  1975. skb_queue_head_init(&l2->down_queue);
  1976. skb_queue_head_init(&l2->tmp_queue);
  1977. InitWin(l2);
  1978. l2->l2m.fsm = &l2fsm;
  1979. if (test_bit(FLG_LAPB, &l2->flag) ||
  1980. test_bit(FLG_FIXED_TEI, &l2->flag) ||
  1981. test_bit(FLG_LAPD_NET, &l2->flag))
  1982. l2->l2m.state = ST_L2_4;
  1983. else
  1984. l2->l2m.state = ST_L2_1;
  1985. l2->l2m.debug = *debug;
  1986. l2->l2m.userdata = l2;
  1987. l2->l2m.userint = 0;
  1988. l2->l2m.printdebug = l2m_debug;
  1989. mISDN_FsmInitTimer(&l2->l2m, &l2->t200);
  1990. mISDN_FsmInitTimer(&l2->l2m, &l2->t203);
  1991. return l2;
  1992. }
  1993. static int
  1994. x75create(struct channel_req *crq)
  1995. {
  1996. struct layer2 *l2;
  1997. if (crq->protocol != ISDN_P_B_X75SLP)
  1998. return -EPROTONOSUPPORT;
  1999. l2 = create_l2(crq->ch, crq->protocol, 0, 0, 0);
  2000. if (!l2)
  2001. return -ENOMEM;
  2002. crq->ch = &l2->ch;
  2003. crq->protocol = ISDN_P_B_HDLC;
  2004. return 0;
  2005. }
  2006. static struct Bprotocol X75SLP = {
  2007. .Bprotocols = (1 << (ISDN_P_B_X75SLP & ISDN_P_B_MASK)),
  2008. .name = "X75SLP",
  2009. .create = x75create
  2010. };
  2011. int
  2012. Isdnl2_Init(u_int *deb)
  2013. {
  2014. debug = deb;
  2015. mISDN_register_Bprotocol(&X75SLP);
  2016. l2fsm.state_count = L2_STATE_COUNT;
  2017. l2fsm.event_count = L2_EVENT_COUNT;
  2018. l2fsm.strEvent = strL2Event;
  2019. l2fsm.strState = strL2State;
  2020. mISDN_FsmNew(&l2fsm, L2FnList, ARRAY_SIZE(L2FnList));
  2021. TEIInit(deb);
  2022. return 0;
  2023. }
  2024. void
  2025. Isdnl2_cleanup(void)
  2026. {
  2027. mISDN_unregister_Bprotocol(&X75SLP);
  2028. TEIFree();
  2029. mISDN_FsmFree(&l2fsm);
  2030. }